第一次过这关应该是在一个月前,那时头脑混成一锅粥,一点印象也没有,这次又重做了本题,而这次感觉面向对象变得明朗了,心情也不一样了,如有错误语法我在下次更新会改掉
package case1; public class TestPersonDemo{ public static void main(String[] args) { /********* begin *********/ // 声明并实例化一Person对象p Person p = new Person(); p.setName("张三"); p.setAge(18); p.talk(); } } // 在这里定义Person类 class Person { /********* begin *********/ private String name; private int age; public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } void talk(){ System.out.print("我是:"+ name + ",今年:" + age + "岁"); } }
package case2; public class extendsTest { public static void main(String args[]) { // 实例化一个Cat对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息 /********* begin *********/ Cat cat = new Cat(); cat.setName("大花猫"); cat.setAge(6); cat.voice(); cat.eat(); /********* end *********/ // 实例化一个Dog对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息 /********* begin *********/ Dog dog = new Dog(); dog.setName("大黑狗"); dog.setAge(8); dog.voice(); dog.eat(); /********* end *********/ } } class Animal { /********* begin *********/ public String name; public int age; public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } void voice(){} void eat(){} /********* end *********/ } class Cat extends Animal { // 定义Cat类的voice()和eat()方法 /********* begin *********/ void voice(){ System.out.println(name + "喵喵叫"); } void eat(){ System.out.println(name + "吃鱼\n" + name + age + "岁"); } /********* end *********/ } class Dog extends Animal { // 定义Dog类的voice()和eat()方法 /********* begin *********/ void voice(){ System.out.println(name + "汪汪叫"); } void eat(){ System.out.println(name + "吃骨头\n" + name + age + "岁"); } /********* end *********/ }
package case3; public class superTest { public static void main(String[] args) { // 实例化一个Student类的对象s,为Student对象s中的school赋值,打印输出信息 /********* begin *********/ Student s = new Student("张三",18); s.school = "哈佛大学"; s.print(); /********* end *********/ } } class Person { /********* begin *********/ String name; int age; Person(String name,int age){ this.name = name; this.age = age; } /********* end *********/ } class Student extends Person { /********* begin *********/ Student(String name,int age){ super(name,age); } String school; void print(){ System.out.println("姓名:"+name+",年龄:"+age+",学校:"+school); } /********* end *********/ }
package case4; public class overridingTest{ public static void main(String[] args){ Student s = new Student("张三",18,"哈佛大学"); s.talk(); } } class Person{ public String name; //公共成员变量name public int age; //公共成员变量age void talk(){ System.out.println("我是:"+name+",今年:"+age+"岁"); //这里不能用school } } class Student extends Person{ //继承父类 String school; Student(String name,int age,String school){ //3个参数的构造方法 this.name = name; this.age = age; this.school = school; } void Student(){ //空参构造方法 } void talk(){ //重写talk方法 System.out.println("我是:"+name+",今年:"+age+"岁,我在"+school+"上学"); } }
package case5; public class abstractTest { public static void main(String[] args) { /********* begin *********/ // 分别实例化Student类与Worker类的对象,并调用各自构造方法初始化类属性。 Student stu = new Student("张三",20,"学生"); Worker p = new Worker("李四",30,"工人"); // 分别调用各自类中被复写的talk()方法 打印信息。 stu.talk(); p.talk(); /********* end *********/ } } // 声明一个名为Person的抽象类,在Person中声明了三个属性name age occupation和一个抽象方法——talk()。 abstract class Person { /********* begin *********/ String name ; int age; String occupation; abstract void talk(); /********* end *********/ } // Student类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息 class Student extends Person { /********* begin *********/ Student(String name,int age,String occupation){ this.name = name; this.age = age; this.occupation = occupation; } void talk(){ System.out.println("学生——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!"); } /********* end *********/ } // Worker类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息 class Worker extends Person { /********* begin *********/ Worker(String name,int age,String occupation){ this.name = name; this.age = age; this.occupation = occupation; } void talk(){ System.out.println("工人——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!"); } /********* end *********/ }
package case6; public class finalTest { public static void main(String args[]) { Bike1 obj = new Bike1(); obj.run(); Honda honda = new Honda(); honda.run(); Yamaha yamaha = new Yamaha(); yamaha.run(); } } //不可以修改 final 变量的值 // final方法,不可以重写 不可以扩展 final 类 //请在此添加你的代码 /********** Begin *********/ class Bike1 { int speedlimit = 90; void run() { speedlimit = 120; System.out.println("speedlimit=120"); } } class Bike2 { void run() { System.out.println("running"); } } class Honda extends Bike2 { void run() { System.out.println("running safely with 100kmph"); } } class Bike3 { } class Yamaha extends Bike3 { void run() { System.out.println("running safely with 100kmph"); } } /********** End **********/
package case7; public class interfaceTest { public static void main(String[] args) { // 实例化一Student的对象s,并调用talk()方法,打印信息 /********* begin *********/ Student s = new Student(); System.out.println(s.talk()); /********* end *********/ } } // 声明一个Person接口,并在里面声明三个常量:name、age和occupation,并分别赋值,声明一抽象方法talk() interface Person { //接口 /********* begin *********/ final String name = "张三"; final int age = 18; final String occupation = "学生"; abstract String talk(); /********* end *********/ } // Student类继承自Person类 复写talk()方法返回姓名、年龄和职业信息 class Student implements Person { /********* begin *********/ public String talk(){ // System.out.println("学生"); return "学生——>姓名:" + name + ",年龄:" + age + ",职业:" + occupation + "!"; } /********* end *********/ }
package case8; public class TestPolymorphism { public static void main(String[] args) { // 以多态方式分别实例化子类对象并调用eat()方法 /********* begin *********/ Animal a1 = new Dog(); Animal a2 = new Cat(); Animal a3 = new Lion(); a1.eat(); a2.eat(); a3.eat(); /********* end *********/ } } // Animal类中定义eat()方法 abstract class Animal { /********* begin *********/ abstract void eat(); /********* end *********/ } // Dog类继承Animal类 复写eat()方法 class Dog extends Animal { /********* begin *********/ void eat(){ System.out.println("eating bread..."); } /********* end *********/ } // Cat类继承Animal类 复写eat()方法 class Cat extends Animal { /********* begin *********/ void eat(){ System.out.println("eating rat..."); } /********* end *********/ } // Lion类继承Animal类 复写eat()方法 class Lion extends Animal { /********* begin *********/ void eat(){ System.out.println("eating meat..."); } /********* end *********/ }