Example5_1
package england.people; import american.people.Son; import japan.people.Grandson; public class Example5_1 { public static void main(String args[]){ Son son = new Son(); Grandson grandson = new Grandson(); son.height = 180; son.hand = "一双大手"; grandson.height = 155; grandson.hand = "一双小手"; grandson.food = "一双小脚"; String str = son.getHand(); System.out.println("儿子:%是,%d\n",str,son.height); str = grandson.getHand(); System.out.println("孙子:%s,%s,%d\n",str,grandson.foot,grandson.height); } }
Example5_3
class People{ public double x; public void setX(double x){ this.x=x; } public double getDoubleX(){ return x; } } class Student extends people{ int x; public int getX(){ //x=20.56; return x; } } public class Example5_3 { public static void main(String args[]){ Student stu = new Student(); stu.x=98; System.out.println("对象stu的x的值是:"+stu.getX()); stu.setX(98.98); double m = stu.getDoubieX(); System.out.println("对象stu隐藏的x的值是:"+m); } }
Example5_4
class A{ double f(float x,float y){ return x+y; } public int g(int x,int y){ return x+y; } } class B extends A{ double f(float x,float y){ return x*y; } }public class Example5_4 { public static void main(String args[]){ B b=new B(); double result = b.f(5,6); System.out.println("调用重写方法得到的结果:"+result); int m = (3,5); System.out.println("调用继承方法得到的结果:"+result); } }
Example5_6
public class Example5_6 { public static void main(String args[]){ UniverStudent zhang = new UniverStudent(20111,"张三",false); int number = zhang.getNumber(); String name = zhang.getName(); boolean marriage = zhang.getIsMarriage(); System.out.println(name+"的学号是:"+number); if(marriage==true){ System.out.println(name+"已婚"); } else{ System.out.println(name+"未婚"); } } }
Example5_7
public class Example5_7 { public static void main(String args[]){ Average aver = new Average(); aver.n = 100.5678; double result1 = aver.f(); double result2 = aver.g(); System.out.println("result1="+result1); System.out.println("result2="+result2); } }
Example5_9
public class Example5_9 { public static void main(String args[]){ People people = new People(); Anthropoid monkey = people; monkey.crySpeak("I love this game"); //monkey.n = 100; //monkey.computer(12,19); System.out.println(monkey.m); System.out.println(people.m); People zhang = (People)monkey; zhang.computer(55,33); zhang.m='T'; System.out.println(zhang.m); } }
Example5_11
bstract class GirlFriend{ abstract void speak(); abstract void cooking(); } class ChinaGirlFriend extends GirlFriend{ void speak(){ System.out.println("你好"); } void cooking(){ System.out.println("水煮鱼"); } } class AmericanGirlFriend extends GirlFriend{ void speak(){ System.out.println("hello"); } void cooking(){ System.out.println("roast beef"); } } class Boy{ GirlFriend friend; void setGirlfriend(GirlFriend f){ friend = f; } } public class Example5_11 { public static void main(String args[]){ GirlFriend girl = new ChinaGirlFriend(); Boy boy = new Boy(); boy.setGirlfriend(girl); boy.showGirlFriend(); girl = new AmericanGirlFriend(); boy.setGirlfriend(girl); boy.showGirlFriend(); } }
Example5_13
public class Example5_13 { public static void main(String args[]){ Simulator simulator = new Simulator(); simulator.playSound(new Dog()); simulator.playSound(new Cat()); } }
Example5_14
public class Example5_14 { public static void main(String args[]) { AAA a = new AAA(); System.out.println("接口中的常量" + AAA.MAX); System.out.println("调用on方法(重写的):"); a.on(); System.out.println("调用sum方法(重写的):" + a.sum(12, 18)); System.out.println("调用接口提供的default方法" + a.max(12, 78)); Printable.f(); } }
习题5
public class A { public final void f() { char cStart = 'a', cEnd = 'z'; for (char c=cStart;c<= cEnd;c++) { System.out.print("" + c); } } } class B extends A { public void g() { char eStart = 'a',cEnd ='w'; for (char c = cStart; c <= cEnd; c++) { System.out.print("" + c); } } } public class B{ public static void main ( String args[]){ B b=new B (); b.f(); b.g(); } }