package com.animal; public class Rabbit extends Animal{ int age; String name; char cry; public Rabbit() { super(); // TODO Auto-generated constructor stub } public Rabbit(int age, String name, char cry) { super(); this.age = age; this.name = name; this.cry = cry; } public void cry() { System.out.println("兔子十点睡"); } @Override public void eat() { System.out.println("我是一个rabbit:吃carrot"); } }
package com.animal; public class AnimalTest{ public static void main(String[] args) { //创建动物对象 Animal am=new Animal(); am.age="12"; am.setName("张三"); am.cry(); am.sleep(); am.eat(); System.out.println(am); System.out.println(am.age+"岁"); System.out.println(am.getName()); // Animal car=new Cat(); car.setAge("13"); car.setName("李四"); car.eat(); car.sleep(); car.cry(); System.out.println(car.getName()+"--------------"+car.getAge()); Animal Dog=new Dog(); Dog.setAge("34"); Dog.setName("我是狗"); Dog.eat(); Dog.sleep(); Dog.cry(); System.out.println(Dog.getName()+"--------------"+Dog.getAge()); Animal Duck=new Duck(); Duck.setAge("34"); Duck.setName("我是呀呀"); Duck.eat(); Duck.sleep(); Duck.cry(); System.out.println(Duck.getName()+"--------------"+Duck.getAge()); Animal pig=new Pig(); pig.setAge("34"); pig.setName("我是呀呀"); pig.eat(); pig.sleep(); pig.cry(); System.out.println(pig.getName()+"--------------"+pig.getAge()); Animal Rabbit=new Rabbit(); Rabbit.setAge("34"); Rabbit.setName("我是呀呀"); Rabbit.eat(); Rabbit.sleep(); Rabbit.cry(); System.out.println(Rabbit.getName()+"--------------"+Rabbit.getAge()); } }
1)继承关系是传递的。若类C继承类B,类B继承类A(多层继承),则类C既有从类B那里继承下来的属性与方法,也有从类A那里继承下来的属性与方法,还可以有自己新定义的属性和方法。继承来的属性和方法尽管是隐式的,但仍是类C的属性和方法。继承是在一些比较一般的类的基础上构造、建立和扩充新类的最有效的手段。
(2)继承简化了人们对事物的认识和描述,能清晰体现相关类间的层次结构关系。
(3)继承提供了软件复用功能。若类B继承类A,那么建立类B时只需要再描述与基类(类A)不同的少量特征(数据成员和成员方法)即可。这种做法能减小代码和数据的冗余度,大大增加程序的重用性。
(4)继承通过增强一致性来减少模块间的接口和界面,大大增加了程序的易维护性。
(5)提供多重继承机制。从理论上说,一个类可以是多个一般类的特殊类,它可以从多个一般类中继承属性与方法,这便是多重继承。Java出于安全性和可靠性的考虑,仅支持单重继承,而通过使用接口机制来实现多重继承。
这又是上面的项目结构:通过下面的运行结果自己总结结论出来:
案例六:
上面是运行结果:
package com.jicheng; public class People { public int age ; //年龄 public String sex;//性别 public int height ;//身高 public int weight; //体重 public String name; public People() { super(); // TODO Auto-generated constructor stub } public People(int age, String sex, int height, int weight, String name) { super(); this.age = age; this.sex = sex; this.height = height; this.weight = weight; this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the sex */ public String getSex() { return sex; } /** * @param sex the sex to set */ public void setSex(String sex) { this.sex = sex; } /** * @return the height */ public int getHeight() { return height; } /** * @param height the height to set */ public void setHeight(int height) { this.height = height; } /** * @return the weight */ public int getWeight() { return weight; } /** * @param weight the weight to set */ public void setWeight(int weight) { this.weight = weight; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } public void eat() { System.out.println("吃饭要积极"); } public void sleep() { System.out.println("睡觉要开心,做个美梦哦"); } public void beat() { System.out.println("打嘟嘟 xing xing hubin "); } public void learn() { System.out.println("no pains no gains 学习要积极"); } }
package com.jicheng; public class Solder extends People{ public int age ; //年龄 public String sex;//性别 public int height ;//身高 public int weight; //体重 public String name; public Solder() { super(); // TODO Auto-generated constructor stub } public Solder(int age, String sex, int height, int weight, String name) { super(age, sex, height, weight, name); // TODO Auto-generated constructor stub } public void tranning() { System.out.println("tranning 顺利了"); } public void run() { System.out.println("run run go go"); } public void playgrams() { System.out.println("play play play "); } }
package com.jicheng; public class Teacher extends People{ public int age ; //年龄 public String sex;//性别 public int height ;//身高 public int weight; //体重 public String name; public Teacher() { super(); } public Teacher(int age, String sex, int height, int weight, String name) { super(age, sex, height, weight, name); // TODO Auto-generated constructor stub } public void learnenglish() { System.out.println("can not obery himself is to other"); } public void learnmath() { System.out.println("math 学习数学强思路"); } public void learnchinese() { System.out.println("学习语文提高文化素质"); } }
package com.jicheng; public class PeopleTest { public static void main(String[] args) { //1 People p =new People(); System.out.println("继承用extends修饰,指的是x属于y的一种"); System.out.println("继承是is...........a的关系"); System.out.println("student and teacher or solder是 people的一种"); System.out.println(p); p.name ="小王"; p.height =148; p.weight =67; p.age=89; p.sex="nv"; System.out.println(p.name); System.out.println(p.height+"cm"); System.out.println(p.weight+"kg"); System.out.println(p.age+"nain"); System.out.println(p.sex); p.beat(); p.sleep(); p.eat(); p.learn(); System.out.println("============================================="); //2 Student s =new Student(); System.out.println(s); s.age=20; s.name="限胡"; s.height =178; s.weight =67; s.age=89; s.sex="nan"; System.out.println(s.name); System.out.println(s.height+"cm"); System.out.println(s.weight+"kg"); System.out.println(s.age+"nain"); System.out.println(s.sex); s.eat(); s.beat(); s.learn(); s.dohomework(); System.out.println("================================================="); //3 Teacher t =new Teacher(); System.out.println(t); t.name="户外"; t.age=34; t.height=167; t.weight=64; t.sex="nan"; //成员变量 System.out.println(t.name); System.out.println(t.height+"cm"); System.out.println(t.weight+"kg"); System.out.println(t.age+"nain"); System.out.println(t.sex); //成员方法 t.learnchinese(); t.learnenglish(); t.learnmath(); t.eat(); t.sleep(); t.beat(); t.learn(); System.out.println("===================================="); Solder d =new Solder (); d.name="户外yu"; d.age=34; d.height=167; d.weight=64; d.sex="nan"; System.out.println(d.name); System.out.println(d.height+"cm"); System.out.println(d.weight+"kg"); System.out.println(d.age+"nain"); System.out.println(d.sex); //成员方法 d.playgrams(); d.learn(); d.run(); d.tranning(); d.eat(); d.sleep(); d.beat(); d.learn(); } }
package com.jicheng; public class PeopleTest { public static void main(String[] args) { //1 People p =new People(); System.out.println("继承用extends修饰,指的是x属于y的一种"); System.out.println("继承是is...........a的关系"); System.out.println("student and teacher or solder是 people的一种"); System.out.println(p); p.name ="小王"; p.height =148; p.weight =67; p.age=89; p.sex="nv"; System.out.println(p.name); System.out.println(p.height+"cm"); System.out.println(p.weight+"kg"); System.out.println(p.age+"nain"); System.out.println(p.sex); p.beat(); p.sleep(); p.eat(); p.learn(); System.out.println("============================================="); //2 Student s =new Student(); System.out.println(s); s.age=20; s.name="限胡"; s.height =178; s.weight =67; s.age=89; s.sex="nan"; System.out.println(s.name); System.out.println(s.height+"cm"); System.out.println(s.weight+"kg"); System.out.println(s.age+"nain"); System.out.println(s.sex); s.eat(); s.beat(); s.learn(); s.dohomework(); System.out.println("================================================="); //3 Teacher t =new Teacher(); System.out.println(t); t.name="户外"; t.age=34; t.height=167; t.weight=64; t.sex="nan"; //成员变量 System.out.println(t.name); System.out.println(t.height+"cm"); System.out.println(t.weight+"kg"); System.out.println(t.age+"nain"); System.out.println(t.sex); //成员方法 t.learnchinese(); t.learnenglish(); t.learnmath(); t.eat(); t.sleep(); t.beat(); t.learn(); System.out.println("===================================="); Solder d =new Solder (); d.name="户外yu"; d.age=34; d.height=167; d.weight=64; d.sex="nan"; System.out.println(d.name); System.out.println(d.height+"cm"); System.out.println(d.weight+"kg"); System.out.println(d.age+"nain"); System.out.println(d.sex); //成员方法 d.playgrams(); d.learn(); d.run(); d.tranning(); d.eat(); d.sleep(); d.beat(); d.learn(); } }