1. package com.ykmimi.javabianchengsixiang;
  2. /**
  3. * 形状的继承 随机形状生成器
  4. * @author ukyor
  5. */
  6. import java.util.Random;
  7.  
  8. class Shape{
  9. public void draw() {}
  10. //擦除 erase
  11. public void erase() {}
  12. }
  13.  
  14. //类 圆形 继承自基类 Shape形状
  15. class Circle extends Shape{
  16. public void draw() {System.out.println("Circle.draw()");}
  17. public void erase() {System.out.println("Circle.erase()");}
  18. }
  19. //类 矩形 继承自基类 Shape形状
  20. class Square extends Shape{
  21. public void draw() {System.out.println("Square.draw()");};
  22. public void erase() {System.out.println("Square.erase)");};
  23. }
  24. //类 三角形 继承自基类 Shape形状
  25. class Triangle extends Shape{
  26. public void draw() {System.out.println("Triangle.draw()");};
  27. public void erase() {System.out.println("Triangle.erase)");};
  28. }
  29. //随机形状生成器
  30. class RandomShapeGenerator{
  31. //随机数 (0~(30~110))~(120/200)
  32. private static int randomNumber = (int)((Math.random()*(30+Math.random()*80))+Math.random()*90);
    private Random rand = new Random(randomNumber);
  33. public Shape next() {
  34. switch(rand.nextInt(3)) {
  35. default:
  36. case 0 : return new Circle();
  37. case 1 : return new Square();
  38. case 2 : return new Triangle();
  39. }
  40. }
  41. public void print() {
  42. System.out.println(randomNumber);
  43. }
  44.  
  45. }
  46. public class Shapes {
  47. //创建静态的随机形状生成器的对象 gen
  48. private static RandomShapeGenerator gen = new RandomShapeGenerator();
  49. public static void main(String[] args) {
  50. //声明形状数组 s 长度为 9 ;
  51. Shape[] s = new Shape[9];
  52. for(int i=0;i<s.length;i++)
  53. {
  54. //调用gen.next()方法返回随机数0~2 0 1 2
  55. s[i] = gen.next();
  56. }
  57. //遍历s数组到每个shp
  58. for(Shape shp: s)
  59. {
  60. //每个shp执行draw方法
  61. shp.draw();
  62. }
  63. //输出print()方法中的randomNumber
  64. gen.print();
  65.  
  66. }
  67. }

//

  1. package 随机数;
  2. /**
  3. * 理解随机数,随机数组
  4. * @author ukyozq
  5. */
  6. import java.util.Random;
  7.  
  8. public class RandomNumberOrArray{
  9.  
  10. public static void main(String[] args) {
  11.  
  12. //声明随机数int rand.范围[0,98]
  13. int rand = (int)(Math.random()*99);
  14. //输出,每次运行会输出不同的数字.
  15. System.out.println(rand);
  16.  
  17. //理所当然我们想到要用数组装入更多随机数
  18. int[] randArray = new int[10];
  19. for(int i=0;i<randArray.length;i++)
  20. {
  21. //随机数赋值到数组的每一位
  22. randArray[i]=rand;
  23. }
  24. for(int x:randArray)
  25. {
  26. //输出 但注意:结果的数据int虽然每次运行不同,为随机产生,
  27. //但数组的每一位都是这一个数.也就是说,该数组的每一位,都
  28. //只是赋值了这一个随机数.是相同的.
  29. System.out.print(x+" ");
  30. }
  31.  
  32. //think:那什么办法生产随机的数到数组中呢?生成随机数组?
  33. //方法1,蠢的办法:声明多个rand.(rand1,rand2,rand3)
  34. int rand1 = (int)(Math.random()*99);
  35. int rand2 = (int)(Math.random()*99);
  36. int rand3 = (int)(Math.random()*99);
  37. //此输出确实不相同,将rand1,2,3分别赋值到数组的0,1,2位置上,
  38. //但是该方法蠢到不能再蠢.
  39. System.out.println("\n"+rand1+" "+rand2+" "+rand3);
  40. int rand4 = (int)(Math.random()*99);
  41.  
  42. //在随机的生产中,除了Math还有Random的声明:
  43. Random rand_1 = new Random();
  44. //new Random()方法产生的是伪随机数
  45. System.out.println(rand_1);
  46. //这样输出↑只是输出的该对象而不是数字,输出数字使用方法:
  47. System.out.println(rand_1.nextInt(10));
  48. //↑该方法输出了范围[0,9]的随机数
  49.  
  50. //那么用该方法赋值随机数组:
  51. for(int i=0;i<randArray.length;i++)
  52. {
  53. //随机数赋值到数组的每一位
  54. //nextInt(范围) 为[0,99]范围内的随机数
  55. randArray[i]=rand_1.nextInt(100);
  56. }
  57. for(int x:randArray)
  58. {
  59. System.out.print(x+" ");
  60. }
  61.  
  62. }
  63. }

帮助大家理解java中的随机和继承,动态绑定.的更多相关文章

  1. 深入理解Java中的IO

    深入理解Java中的IO 引言:     对程序语言的设计者来说,创建一个好的输入/输出(I/O)系统是一项艰难的任务 < Thinking in Java >   本文的目录视图如下: ...

  2. 理解Java中的弱引用(Weak Reference)

    本篇文章尝试从What.Why.How这三个角度来探索Java中的弱引用,理解Java中弱引用的定义.基本使用场景和使用方法.由于个人水平有限,叙述中难免存在不准确或是不清晰的地方,希望大家可以指出, ...

  3. 深入理解Java中的继承

    对于面向对象的程序设计而言,每一个程序员都应该去了解Java中的封装,继承和多态,那么我今天来说的主要是以继承为核心的主题. 一.关于对继承的理解. 继承是面向对象的三大特性之一,是java中实现代码 ...

  4. 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因

    声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...

  5. [译]线程生命周期-理解Java中的线程状态

    线程生命周期-理解Java中的线程状态 在多线程编程环境下,理解线程生命周期和线程状态非常重要. 在上一篇教程中,我们已经学习了如何创建java线程:实现Runnable接口或者成为Thread的子类 ...

  6. 深入理解Java中的不可变对象

    深入理解Java中的不可变对象 不可变对象想必大部分朋友都不陌生,大家在平时写代码的过程中100%会使用到不可变对象,比如最常见的String对象.包装器对象等,那么到底为何Java语言要这么设计,真 ...

  7. 理解Java中的ThreadLocal

    提到ThreadLocal,有些Android或者Java程序员可能有所陌生,可能会提出种种问题,它是做什么的,是不是和线程有关,怎么使用呢?等等问题,本文将总结一下我对ThreadLocal的理解和 ...

  8. 深入理解Java中配置环境变量

    深入理解Java中配置环境变量 配置的目的: 本来只在安装JDK的bin目下能运行java.exe,javac.exe,jar.exe,javadoc.exe等Java开发工具包命令,我们现在想让在所 ...

  9. 十分钟理解Java中的动态代理

    十分钟理解 Java 中的动态代理   一.概述 1. 什么是代理 我们大家都知道微商代理,简单地说就是代替厂家卖商品,厂家“委托”代理为其销售商品.关于微商代理,首先我们从他们那里买东西时通常不知道 ...

随机推荐

  1. 【虫师讲Selenium+Python】第三讲:操作测试对象

    一.首先呢,选择一个编辑器,我们这里选择的是Sublime Text >Ctrl+B为运行当前脚本的快捷方式 二.编写代码 #coding==utf-8 from selenium import ...

  2. InnoSQL HA Suite的实现原理与配置说明 InnoSQL的VSR功能Virtual Sync Replication MySQL 5.5版本引入了半同步复制(semi-sync replicaiton)的功能 MySQL 5.6支持了crash safe功能

    InnoSQL HA Suite的实现原理与配置说明  InnoSQL的VSR功能Virtual Sync Replication MySQL 5.5版本引入了半同步复制(semi-sync repl ...

  3. vue.set的用法

    Vue.set(this.food,'count',1) //就是给this.food里面添加一个count的属性,并且赋值为1

  4. SparkSQL UDF两种注册方式:udf() 和 register()

    调用sqlContext.udf.register() 此时注册的方法 只能在sql()中可见,对DataFrame API不可见 用法:sqlContext.udf.register("m ...

  5. [vue]vue双向绑定$on $emit sync-模态框

    双向绑定实现($on $emit) 关于父子之间数据更新同步, 如果单向绑定, 子修改了,父却没有修改, 这种一般不符合规范 正常更新数据的套路是: 1. 子通知父更新数据 2. 子自动刷新获取最新数 ...

  6. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  7. Ghost硬盘对拷

    Ghost硬盘对拷 优点:全盘完全100%对拷,包括原有操作系统也可使用.新硬盘对拷结束后,可直接插上电脑使用.消耗时间最短. 困难:对于第一次操作Ghost对拷的新人来说,需要严格对照图片步骤教程. ...

  8. jQuery在iframe里取得父窗口的某个元素的值

    提供一款jQuery在iframe里取得父窗口的某个元素的值实现,这个iframe用js也差不多,有需要的朋友可以参考一下. 1.在父窗口中获取指定iframe(testiframe) id 为 te ...

  9. TempData["a"]多个Action方法之前共享数据

    ViewData["a"]只可以在自己视图的页面里被访问,但TempData["a"]可以多个Action方法之前共享数据,比如在 @{Html.RenderA ...

  10. Nhibernate入门与demo

    学习和使用Nhibernate已经很久了,一直想写点东西和大家一起学习使用Nhibernate.博客园里也有很多大牛写了很多关于Nhibernate入门的文章.其中:李永京的博客http://www. ...