学习java基础

1. Static and Final Keywords

private staticRandomrand=newRandom();

static classJob {

private final inta;

/*private final static int a;

ERROR:Can not add static because static

member variable will be initialized

before the constructor loaded*/

Job() {

a=rand.nextInt(12);//Final member variable initialized here

}

intGet(){

return this.a;

}

}

public static voidmain(String[] args) {

Job a =newJob();

Job b =newJob();

print(a.Get());

print(b.Get());

}

2.Polymorphism and Dynamic Binding

importjava.util.Random;

import staticnet.mindview.util.Print.print;

classRodent{

public voidmyFood(){};

}

classMouseextendsRodent{

public voidmyFood(){

print("Grain is my favorite");

}

}

classGerbilextendsRodent{

public voidmyFood(){

print("Grass is my favorite");

}

}

classHamsterextendsRodent{

public voidmyFood(){

print("Peanut is my favorite");

}

}

classRodentParadise{

privateRodent[]buildingNest() {

Random rand =newRandom();

Rodent[] myWarmNest =newRodent[7];

for(inti =0;i < myWarmNest.length;i++){

switch(rand.nextInt(3)){

default:

case0:myWarmNest[i] =newMouse();continue;

case1:myWarmNest[i] =newGerbil();continue;

case2:myWarmNest[i] =newHamster();continue;

}

}

returnmyWarmNest;

}

publicRodent[]getNest(){

returnbuildingNest();

}

}

public classExercise {

public static voidmain(String[] args) {

RodentParadise n =newRodentParadise();

for(Rodent x:n.getNest()){

x.myFood();

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,975评论 0 2
  • 第一阶段题库 基础知识部分: 1. JDK是什么?JRE是什么? a) 答:JDK:java开发工具包。JRE:j...
    紫豆包阅读 1,873评论 1 27
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,839评论 18 399
  • 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔...
    叶总韩阅读 5,186评论 0 41
  • 第一次去新疆学读了一个字“很”(鼻音四声),概括一下就是新疆:风景美的很、天气冷的很、人热情的很、路远的很、面积大...
    近琢阅读 746评论 5 4