案例九:综合案例二:
package com.JavaBasicsDemo6.Abstract8; public class Animal { public int age = 40; public String name; public Animal() { super(); // TODO Auto-generated constructor stub } public Animal(String name) { super(); this.name = name; } public void eat(){ System.out.println("动物吃东西"); } } package com.JavaBasicsDemo6.Abstract8; public class Cat extends Animal{ public int age = 15; public int weight = 10; public Cat() { } public Cat(String name) { super(name); } @Override public void eat(){ // TODO Auto-generated method stub System.out.println("猫抓老鼠"); } public void playGame(){ System.out.println("猫捉迷藏"); } } package com.JavaBasicsDemo6.Abstract8; public class Dog extends Animal{ public void guard(){ } } package com.JavaBasicsDemo6.Abstract8; public class AnimalTest { public static void main(String[] args) { // 创建一个Animal对象 Animal a = new Animal(); //创建一只猫 Cat c = new Cat("猫"); thinking(c); //父类型的引用接收了(指向)子类型的对象,称为向上造型 Animal a1 = new Dog(); System.out.println(a1.age); a1.eat(); // Cat c1 = (Cat)new Animal(); } public static void thinking(Animal animal) {//各种动物在思考Animal animal = new Cat(); if (animal.name == "猫") { System.out.println("猫在思考"); } if (animal.name == "狗狗") { System.out.println("狗在思考"); } } }
案例十:综合案例三:
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 = 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("---------------------------------"); } }