帮助大家理解java中的随机和继承,动态绑定.
- package com.ykmimi.javabianchengsixiang;
- /**
- * 形状的继承 随机形状生成器
- * @author ukyor
- */
- import java.util.Random;
- class Shape{
- public void draw() {}
- //擦除 erase
- public void erase() {}
- }
- //类 圆形 继承自基类 Shape形状
- class Circle extends Shape{
- public void draw() {System.out.println("Circle.draw()");}
- public void erase() {System.out.println("Circle.erase()");}
- }
- //类 矩形 继承自基类 Shape形状
- class Square extends Shape{
- public void draw() {System.out.println("Square.draw()");};
- public void erase() {System.out.println("Square.erase)");};
- }
- //类 三角形 继承自基类 Shape形状
- class Triangle extends Shape{
- public void draw() {System.out.println("Triangle.draw()");};
- public void erase() {System.out.println("Triangle.erase)");};
- }
- //随机形状生成器
- class RandomShapeGenerator{
- //随机数 (0~(30~110))~(120/200)
- private static int randomNumber = (int)((Math.random()*(30+Math.random()*80))+Math.random()*90);
private Random rand = new Random(randomNumber);- public Shape next() {
- switch(rand.nextInt(3)) {
- default:
- case 0 : return new Circle();
- case 1 : return new Square();
- case 2 : return new Triangle();
- }
- }
- public void print() {
- System.out.println(randomNumber);
- }
- }
- public class Shapes {
- //创建静态的随机形状生成器的对象 gen
- private static RandomShapeGenerator gen = new RandomShapeGenerator();
- public static void main(String[] args) {
- //声明形状数组 s 长度为 9 ;
- Shape[] s = new Shape[9];
- for(int i=0;i<s.length;i++)
- {
- //调用gen.next()方法返回随机数0~2 0 1 2
- s[i] = gen.next();
- }
- //遍历s数组到每个shp
- for(Shape shp: s)
- {
- //每个shp执行draw方法
- shp.draw();
- }
- //输出print()方法中的randomNumber
- gen.print();
- }
- }
//
- package 随机数;
- /**
- * 理解随机数,随机数组
- * @author ukyozq
- */
- import java.util.Random;
- public class RandomNumberOrArray{
- public static void main(String[] args) {
- //声明随机数int rand.范围[0,98]
- int rand = (int)(Math.random()*99);
- //输出,每次运行会输出不同的数字.
- System.out.println(rand);
- //理所当然我们想到要用数组装入更多随机数
- int[] randArray = new int[10];
- for(int i=0;i<randArray.length;i++)
- {
- //随机数赋值到数组的每一位
- randArray[i]=rand;
- }
- for(int x:randArray)
- {
- //输出 但注意:结果的数据int虽然每次运行不同,为随机产生,
- //但数组的每一位都是这一个数.也就是说,该数组的每一位,都
- //只是赋值了这一个随机数.是相同的.
- System.out.print(x+" ");
- }
- //think:那什么办法生产随机的数到数组中呢?生成随机数组?
- //方法1,蠢的办法:声明多个rand.(rand1,rand2,rand3)
- int rand1 = (int)(Math.random()*99);
- int rand2 = (int)(Math.random()*99);
- int rand3 = (int)(Math.random()*99);
- //此输出确实不相同,将rand1,2,3分别赋值到数组的0,1,2位置上,
- //但是该方法蠢到不能再蠢.
- System.out.println("\n"+rand1+" "+rand2+" "+rand3);
- int rand4 = (int)(Math.random()*99);
- //在随机的生产中,除了Math还有Random的声明:
- Random rand_1 = new Random();
- //new Random()方法产生的是伪随机数
- System.out.println(rand_1);
- //这样输出↑只是输出的该对象而不是数字,输出数字使用方法:
- System.out.println(rand_1.nextInt(10));
- //↑该方法输出了范围[0,9]的随机数
- //那么用该方法赋值随机数组:
- for(int i=0;i<randArray.length;i++)
- {
- //随机数赋值到数组的每一位
- //nextInt(范围) 为[0,99]范围内的随机数
- randArray[i]=rand_1.nextInt(100);
- }
- for(int x:randArray)
- {
- System.out.print(x+" ");
- }
- }
- }
帮助大家理解java中的随机和继承,动态绑定.的更多相关文章
- 深入理解Java中的IO
深入理解Java中的IO 引言: 对程序语言的设计者来说,创建一个好的输入/输出(I/O)系统是一项艰难的任务 < Thinking in Java > 本文的目录视图如下: ...
- 理解Java中的弱引用(Weak Reference)
本篇文章尝试从What.Why.How这三个角度来探索Java中的弱引用,理解Java中弱引用的定义.基本使用场景和使用方法.由于个人水平有限,叙述中难免存在不准确或是不清晰的地方,希望大家可以指出, ...
- 深入理解Java中的继承
对于面向对象的程序设计而言,每一个程序员都应该去了解Java中的封装,继承和多态,那么我今天来说的主要是以继承为核心的主题. 一.关于对继承的理解. 继承是面向对象的三大特性之一,是java中实现代码 ...
- 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因
声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...
- [译]线程生命周期-理解Java中的线程状态
线程生命周期-理解Java中的线程状态 在多线程编程环境下,理解线程生命周期和线程状态非常重要. 在上一篇教程中,我们已经学习了如何创建java线程:实现Runnable接口或者成为Thread的子类 ...
- 深入理解Java中的不可变对象
深入理解Java中的不可变对象 不可变对象想必大部分朋友都不陌生,大家在平时写代码的过程中100%会使用到不可变对象,比如最常见的String对象.包装器对象等,那么到底为何Java语言要这么设计,真 ...
- 理解Java中的ThreadLocal
提到ThreadLocal,有些Android或者Java程序员可能有所陌生,可能会提出种种问题,它是做什么的,是不是和线程有关,怎么使用呢?等等问题,本文将总结一下我对ThreadLocal的理解和 ...
- 深入理解Java中配置环境变量
深入理解Java中配置环境变量 配置的目的: 本来只在安装JDK的bin目下能运行java.exe,javac.exe,jar.exe,javadoc.exe等Java开发工具包命令,我们现在想让在所 ...
- 十分钟理解Java中的动态代理
十分钟理解 Java 中的动态代理 一.概述 1. 什么是代理 我们大家都知道微商代理,简单地说就是代替厂家卖商品,厂家“委托”代理为其销售商品.关于微商代理,首先我们从他们那里买东西时通常不知道 ...
随机推荐
- 【虫师讲Selenium+Python】第三讲:操作测试对象
一.首先呢,选择一个编辑器,我们这里选择的是Sublime Text >Ctrl+B为运行当前脚本的快捷方式 二.编写代码 #coding==utf-8 from selenium import ...
- 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 ...
- vue.set的用法
Vue.set(this.food,'count',1) //就是给this.food里面添加一个count的属性,并且赋值为1
- SparkSQL UDF两种注册方式:udf() 和 register()
调用sqlContext.udf.register() 此时注册的方法 只能在sql()中可见,对DataFrame API不可见 用法:sqlContext.udf.register("m ...
- [vue]vue双向绑定$on $emit sync-模态框
双向绑定实现($on $emit) 关于父子之间数据更新同步, 如果单向绑定, 子修改了,父却没有修改, 这种一般不符合规范 正常更新数据的套路是: 1. 子通知父更新数据 2. 子自动刷新获取最新数 ...
- [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 ...
- Ghost硬盘对拷
Ghost硬盘对拷 优点:全盘完全100%对拷,包括原有操作系统也可使用.新硬盘对拷结束后,可直接插上电脑使用.消耗时间最短. 困难:对于第一次操作Ghost对拷的新人来说,需要严格对照图片步骤教程. ...
- jQuery在iframe里取得父窗口的某个元素的值
提供一款jQuery在iframe里取得父窗口的某个元素的值实现,这个iframe用js也差不多,有需要的朋友可以参考一下. 1.在父窗口中获取指定iframe(testiframe) id 为 te ...
- TempData["a"]多个Action方法之前共享数据
ViewData["a"]只可以在自己视图的页面里被访问,但TempData["a"]可以多个Action方法之前共享数据,比如在 @{Html.RenderA ...
- Nhibernate入门与demo
学习和使用Nhibernate已经很久了,一直想写点东西和大家一起学习使用Nhibernate.博客园里也有很多大牛写了很多关于Nhibernate入门的文章.其中:李永京的博客http://www. ...