案例一继承的内容。弄清楚主次关系。谁继承谁。
代码实例。
package extends1; /** * 人类 * @author MZFAITHDREAM * */ public class Person { private String name; private int age; public Person() { super(); // TODO Auto-generated constructor stub } public Person(String name, int age) { super(); this.name = name; this.age = 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; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
package extends1; /** * 定义学生类(姓名,年龄,教书()) * @author Administrator * */ public class Student extends Person{ public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(name, age); } public void study(){ System.out.println("学生学习"); } }
package extends1; /** * 定义老师的类名 * @author MZFAITHDREAM * */ public class Teacher extends Person{ public Teacher() { super(); // TODO Auto-generated constructor stub } public Teacher(String name, int age) { super(name,age); } public void teach() { System.out.println("用爱给每一个学生"); } }
上面的案例中有人类 老师 学生。
案例二在上面的基础上在扩展开来。请看下面的案例。
package com.animal; /** * 一个主人养了猫和狗,猫和狗都有自己爱吃的东西,主人在喂它们 * 的时候,如果既要判断是猫还是狗,再判断他们分别爱吃什么,就显得很麻烦。 * 如果主人养了很多种动物,这样的重复判断,就会浪费很多时间。有什么办法, * 能让主人拿到一种食物就知道这是哪种动物的,就好了。 * @author MZFAITHDREAM * */ public class Animal { public String age; public String name; public char cry; public Animal() { super(); // TODO Auto-generated constructor stub } public Animal(String age, String name, char cry) { super(); this.age = age; this.name = name; this.cry = cry; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } // 动物类里面有叫和吃两个方法 public void cry() { System.out.println("我不知道叫什么"); } public void eat() { System.out.println("我不知道吃什么"); } public void sleep() { System.out.println("我不知道几点睡觉"); } }
package com.animal; public class Cat extends Animal{ int age; String name; char cry; public Cat() { super(); // TODO Auto-generated constructor stub } public Cat(int age, String name, char cry) { super(); this.age = age; this.name = name; this.cry = cry; } public void cry() { System.out.println("喵喵"); } public void eat() { System.out.println("我是猫,我爱吃鱼"); } public void sleep() { System.out.println("我是猫,5:00睡觉"); } } package com.animal; public class Dog extends Animal{ int age; String name; char cry; public Dog() { super(); // TODO Auto-generated constructor stub } public Dog(int age, String name, char cry) { super(); this.age = age; this.name = name; this.cry = cry; } // 覆盖(重写)方法 public void cry() { System.out.println("旺旺"); } public void eat() { System.out.println("我是狗,我爱吃骨头"); } public void sleep() { System.out.println("我是狗,7:00睡觉"); } } package com.animal; public class Duck extends Animal{ int age; String name; char cry; public Duck() { super(); // TODO Auto-generated constructor stub } public Duck(int age, String name, char cry) { super(); this.age = age; this.name = name; this.cry = cry; } @Override public void cry() { System.out.println("duck duck"); } @Override public void eat() { System.out.println("我是鸭子 :爱吃vegetanles"); } } package com.animal; public class Pig extends Animal{ int age; String name; char cry; public Pig() { super(); // TODO Auto-generated constructor stub } public Pig(int age, String name, char cry) { super(); this.age = age; this.name = name; this.cry = cry; } @Override public void cry() { System.out.println("pig pigpig"); } @Override public void eat() { System.out.println("我是一个猪:吃potherlb"); } } 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("pig pigpig"); } @Override public void eat() { System.out.println("我是一个rabbit:吃carrot"); } }
自己用分析一下上面的案例
多态一个物体的多中形态。
案例介绍。
package abstact; /** * 抽象类不能被实例化(初学者很容易犯的错),如果被实例化,就会报错,编译无法通过。只有抽象类的非抽象子类可以创建对象。 抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类。 抽象类中的抽象方法只是声明,不包含方法体,就是不给出方法的具体实现也就是方法的具体功能。 构造方法,类方法(用 static 修饰的方法)不能声明为抽象方法。 抽象类的子类必须给出抽象类中的抽象方法的具体实现,除非该子类也是抽象类。 * @author MZFAITHDREAM * @author MZFAITHDREAM * */ public abstract class Student { //姓名 private String name; //年龄 private int age; //班级 private String grand; public Student(){} public Student(String name,int age,String grand){ this.name = name; this.age = age; this.grand = grand; } //getXxx()/setXxx() 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; } public String getGrand(){ return grand; } public void setGrand(String grand){ this.grand = grand; } //学习study(抽象方法) (抽象方法) public abstract void study(); //学习打王者游戏(抽象方法 playingking) public abstract void playingking(); //学习english (抽象方法) public abstract void learnenglish(); //吃饭eat(具体方法) public void eat(){ System.out.println("学习累了,去吃饭"); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((grand == null) ? 0 : grand.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (grand == null) { if (other.grand != null) return false; } else if (!grand.equals(other.grand)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", grand=" + grand + "]"; } }
package abstact; //定义具体基础班学员类 public class BasicStudent extends Student{ public BasicStudent(){ super(); } public void study(){ System.out.println("基础班学员学习的是JavaSE"); } public void playingking() { System.out.println("学会一点游戏,不是很好"); } public void learnenglish() { System.out.println("英语学的是皮毛"); } } package abstact; public class OutstandingStudent extends Student{ public OutstandingStudent() {} public OutstandingStudent(String name, int age, String grand) { super(name, age, grand); } @Override public void study() { System.out.println("好好学习,天天向上"); } @Override public void playingking() { System.out.println("王者是什么呢"); } @Override public void learnenglish() { System.out.println("english很厉害"); } } package abstact; //定义具体就业班学员类 public class WorkStudent extends Student{ public WorkStudent(){} public WorkStudent(String name,int age,String grand){ super(name,age,grand); } public void study(){ System.out.println("就业班学员学习的是JavaEE"); } @Override public void playingking() { System.out.println("游戏上王者,学业一事无成"); } @Override public void learnenglish() { System.out.println("I don't like english"); } }
package abstact; public class StudentTest { /** * abstact 父类中定义,子类中去实现,Test 中去测试 * @param args */ public static void main(String[] args) { //基础班学员测试 //多态(测试) //方式1 Student s1 = new BasicStudent(); s1.setName("一日王月"); s1.setGrand("bdqn_S1"); System.out.println(s1.getName()+"---"+s1.getAge()+"---"+s1.getGrand()); s1.study(); s1.eat(); s1.learnenglish(); s1.playingking(); System.out.println("---------------------------------"); //方式2 s1.setName("一日王月a"); s1 = new BasicStudent(); System.out.println(s1.getName()+"---"+s1.getAge()+"---"+s1.getGrand()); s1.study(); s1.eat(); s1.learnenglish(); s1.playingking(); System.out.println("---------------------------------"); //就业班学员测试 //方式1 Student s = new WorkStudent(); s.setName("我的人生"); s.setAge(25); s.setGrand("bdqn_Y2"); System.out.println(s.getName()+"---"+s.getAge()+"---"+s.getGrand()); s.study(); s.eat(); s.learnenglish(); s.playingking(); System.out.println("---------------------------------"); //方式2 s = new BasicStudent(); System.out.println(s.getName()+"---"+s.getAge()+"---"+s.getGrand()); s.study(); s.eat(); System.out.println(s.hashCode()); System.out.println("---------------------------------"); //优秀学员 //方式1 Student a =new OutstandingStudent(); a.setName("自学成才"); a.setAge(20); a.setGrand("bdqn_S1——阿0298"); System.out.println(a.getName()+"---"+a.getAge()+"---"+a.getGrand()); a.study(); a.eat(); a.learnenglish(); a.playingking(); System.out.println(a.hashCode()); System.out.println("---------------------------------"); //方式2 a = new BasicStudent(); System.out.println(a.getName()+"---"+a.getAge()+"---"+a.getGrand()); a.study(); a.eat(); a.learnenglish(); a.playingking(); System.out.println(s.hashCode()); System.out.println("---------------------------------"); } }
abstact 父类中定义,子类中去实现,Test 中去测试。
多态与继承的融汇使用。
案例
package com.food; public class Food { String name; public Food() { super(); // TODO Auto-generated constructor stub } public Food(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } // 食物类里面让它有一个方法 public void showName() { System.out.println("00000000000"); } } package com.food; public class Bone extends Food{ public void showName() { System.out.println("食物:骨头"); } } package com.food; public class Carrot extends Food{ public void showName() { System.out.println("食物:红萝卜"); } } package com.food; public class Fish extends Food{ public void showName() { System.out.println("食物:鱼"); } } package com.food; public class potherlb extends Food{ @Override public void showName() { System.out.println("食物:potherlb"); } } package com.food; public class Vegetables extends Food{ public void showName() { System.out.println("食物:vegetables"); } } package com.test; import com.animal.Animal; import com.food.Food; public class Master { // 给动物喂食物,如果没有多态,他要写给猫喂食和给狗喂食两个方法 // 有了多态,以后即使再来好多动物,用这一个函数就可以了 public <sleep> void feed(Animal an, Food f ) { an.eat(); f.showName(); } } package com.test; import com.animal.Animal; import com.animal.Cat; import com.animal.Dog; import com.animal.Duck; import com.animal.Pig; import com.animal.Rabbit; import com.food.Bone; import com.food.Carrot; import com.food.Fish; import com.food.Food; import com.food.Vegetables; import com.food.potherlb; /** * 思路 * 1.首先,创造动物类 * 2 其次,分别创造猫类和狗类(他们继承于动物类) * 3 再者,创建食物类: * 4 再者,猫和狗都有自己不同的爱吃的食物(他们继承于食物类) * 5.主人类(就可以将动物和对应的食物统一起来): * 6 最后,方法的调用(测试) * @author MZFAITHDREAM * */ public class DuoTaiDemo { public static void main(String args[]) { Master master = new Master(); master.feed(new Dog(), new Bone()); System.out.println("===================AAAA========================"); master.feed(new Cat(), new Fish()); System.out.println("======================SSSS====================="); master.feed(new Duck(), new Vegetables()); System.out.println("========================DDDD==================="); master.feed(new Pig(), new potherlb()); System.out.println("=========================FFFF=================="); master.feed(new Rabbit(), new Carrot()); System.out.println("++++++++++++++++++++++++++++++++++++++++"); //父类向子类 Animal a =new Cat(); a.name="hihi"; a.age="2"; System.out.println(a.name); System.out.println(a.age); a.cry(); a.eat(); a.sleep(); System.out.println("==========================================="); Animal b =new Dog(); b.cry(); b.eat(); b.sleep(); Animal c =new Duck(); c.eat(); c.cry(); c.sleep(); System.out.println("==========================================="); Animal e=new Pig(); e.eat(); e.cry(); e.sleep(); System.out.println("==========================================="); Animal f =new Rabbit(); f.cry(); f.eat(); f.sleep(); System.out.println("==========================================="); Food ab=new Bone(); ab.showName(); Food bc=new Carrot(); bc.showName(); Food cd=new Fish(); cd.showName(); Food de=new potherlb(); de.showName(); Food ef=new Vegetables(); ef.showName(); // hin方便,可以再试试 } }