1、继承示例
1.1
创建Pet(宠物)父类
// 创建Pet(宠物)父类public class Pet { //创建父类私有属性 private String name; private int age; private int health; private String love; public Pet(){ //System.out.println("父类无参构造方法"); } public Pet(String name,int age,int health,String love){ this.name = name; this.age = age; this.health = health; this.love = love; //System.out.println("父类有参构造方法"); } //get方法 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 int getHealth() { return health; } //set方法 public void setHealth(int health) { this.health = health; } public String getLove() { return love; } public void setLove(String love) { this.love = love; } // 父类中的print(打印)方法 public void print(){ System.out.println(name+"\t"+age+"\t"+health+"\t"+love); } // 父类中的shout(叫声)方法 public void shout(){ System.out.println("宠物的叫声!"); } }
创建Dog(宠物>狗)子类extends关键字
// 创建Dog(宠物>狗)子类继承父类Pet public class Dog extends Pet { // 狗类特有的strain(品种)属性 public String strain; public Dog() { // System.out.println("子类无参构造方法"); } public Dog(String name, int age, int health, String love,String strain) { // super关键字:引用父类中的属性 super(name, age, health, love); this.strain = strain; // System.out.println("子类有参构造方法"); } // 子类get方法 public String getStrain() { return strain; } // 子类set方法 public void setStrain(String strain) { this.strain = strain; } // 父类print方法重写 public void print(){ System.out.println(super.getName()+"\t"+super.getAge()+"\t"+super.getHealth()+"\t"+super.getLove()+"\t"+strain); } // 父类shout方法重写 // public void shout(){// // System.out.println("名叫"+this.getName()+"小狗汪汪的叫声!");// // }// }
// 创建PetDemo测试类
public class PetDemo { public static void main(String[] args) { Dog dog = new Dog("旺财", 2, 100, "友爱", "泰迪");//// dog.setName("旺财");//// dog.strain="泰迪";//// dog.setAge(2);//// dog.setHealth(100);//// dog.setLove("友爱");//// System.out.println(dog.getName() + "\t" + dog.strain + "\t"// // + dog.getAge() + "\t" + dog.getHealth() + "\t" + dog.getLove()); dog.print(); // Penguin penguin = new Penguin(); // Pet pet1 = new Dog(); // Pet pet2 = new Penguin(); // pet1.shout(); // pet2.shout(); // Petshop petshop = new Petshop(); // Pet pet3 = petshop.BuyPet(); // pet3.shout(); // if(pet3 instanceof Dog){ // Dog dog1 = (Dog)pet3; // System.out.println(dog1.getStrain()); // }else{ // Penguin penguin1 = (Penguin)pet3; // System.out.println(penguin1.getSex()); // } // Pet dog = new Dog("旺财",12,100,"友爱","泰迪"); // // dog.shout(); // }}
结果验证一:
旺财2100友爱泰迪
结果验证二:去掉Dog类中super();中内容
null00null泰迪
2、多态示例一
“+”:可以表示加法运算符;可以表示拼接运算符。
2.1
//创建Pet(宠物)父类public class Pet { //创建父类私有属性 private String name; private int age; private int health; private String love; public Pet(){ //System.out.println("父类无参构造方法"); } public Pet(String name,int age,int health,String love){ this.name = name; this.age = age; this.health = health; this.love = love; //System.out.println("父类有参构造方法"); } //get方法 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 int getHealth() { return health; } //set方法 public void setHealth(int health) { this.health = health; } public String getLove() { return love; } public void setLove(String love) { this.love = love; } // 父类中的print(打印)方法 public void print(){ System.out.println(name+"\t"+age+"\t"+health+"\t"+love); } // 父类中的shout(叫声)方法 public void shout(){ System.out.println("宠物的叫声!"); } }
创建Dog(宠物>狗)子类继承父类Pet
// 创建Dog(宠物>狗)子类继承父类Pet public class Dog extends Pet { // 狗类特有的strain(品种)属性 public String strain; public Dog() { // System.out.println("子类无参构造方法"); } public Dog(String name, int age, int health, String love,String strain) { // super关键字:引用父类中的属性 super(name, age, health, love); this.strain = strain; // System.out.println("子类有参构造方法"); } // 子类get方法 public String getStrain() { return strain; } // 子类set方法 public void setStrain(String strain) { this.strain = strain; } // 父类print方法重写// // public void print(){// // System.out.println(super.getName()+"\t"+super.getAge()+"\t"+super.getHealth()+"\t"+super.getLove()+"\t"+strain);// } // 父类shout方法重写 public void shout(){ System.out.println("名叫"+this.getName()+"小狗汪汪的叫声!"); } }
// 创建PetDemo测试类
public class PetDemo { public static void main(String[] args) { // Dog dog = new Dog("旺财",2,100,"友爱","泰迪"); //dog.setName("旺财"); //dog.strain="泰迪"; //dog.setAge(2); //dog.setHealth(100); //dog.setLove("友爱"); // System.out.println(dog.getName()+"\t"+dog.strain+"\t"+dog.getAge()+"\t"+dog.getHealth()+"\t"+dog.getLove());// dog.print(); // Penguin penguin = new Penguin();// Pet pet1 = new Dog(); // Pet pet2 = new Penguin(); // pet1.shout(); // pet2.shout(); // Petshop petshop = new Petshop(); // Pet pet3 = petshop.BuyPet(); // pet3.shout(); // if(pet3 instanceof Dog){ // Dog dog1 = (Dog)pet3;// System.out.println(dog1.getStrain()); // }else{ // Penguin penguin1 = (Penguin)pet3; // System.out.println(penguin1.getSex()); // } Pet dog = new Dog("旺财",12,100,"友爱","泰迪"); dog.shout(); } }
结果验证:
名叫旺财小狗汪汪的叫声!
2.2 多态的示例
创建Pet(宠物)父类
//创建Pet(宠物)父类public class Pet { //创建父类私有属性 private String name; private int age; private int health; private String love; public Pet(){ //System.out.println("父类无参构造方法"); } public Pet(String name,int age,int health,String love){ this.name = name; this.age = age; this.health = health; this.love = love; //System.out.println("父类有参构造方法"); } //get方法 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 int getHealth() { return health; } //set方法 public void setHealth(int health) { this.health = health; } public String getLove() { return love; } public void setLove(String love) { this.love = love; } // 父类中的print(打印)方法 public void print(){ System.out.println(name+"\t"+age+"\t"+health+"\t"+love); } // 父类中的shout(叫声)方法 public void shout(){ System.out.println("宠物的叫声!"); } }
创建Dog(宠物>狗)子类继承父类Pet
// 创建Dog(宠物>狗)子类继承父类Pet public class Dog extends Pet { // 狗类特有的strain(品种)属性 public String strain; public Dog() { // System.out.println("子类无参构造方法"); } public Dog(String name, int age, int health, String love,String strain) { // super关键字:引用父类中的属性 super(name, age, health, love); this.strain = strain; // System.out.println("子类有参构造方法"); } // 子类get方法 public String getStrain() { return strain; } // 子类set方法 public void setStrain(String strain) { this.strain = strain; } // 父类print方法重写// // public void print(){// // System.out.println(super.getName()+"\t"+super.getAge()+"\t"+super.getHealth()+"\t"+super.getLove()+"\t"+strain);// } // 父类shout方法重写 public void shout(){ System.out.println("名叫"+this.getName()+"小狗汪汪的叫声!"); } }
创建Penguin(宠物>企鹅)类
// 创建Penguin(宠物>企鹅)类public class Penguin extends Pet{ // 企鹅sex(性别)特有属性 public String sex; // 子类get方法 public String getSex() { return sex; } // 子类set方法 public void setSex(String sex) { this.sex = sex; } // 父类shout方法重写 public void shout(){ System.out.println("名叫"+this.getName()+"企鹅喵喵的叫声!"); }}
创建Petshop(提供购买的方法)类
//创建Petshop(提供购买的方法)类import java.util.Scanner;public class Petshop {// 创建BuyPet方法 public Pet BuyPet(){ // 定义pet变量的初始值为null(空) Pet pet = null; // 提示用户的输入数字 System.out.println("请输入您要购买的宠物:1.小狗 2.小企鹅"); // 获取用户的输入存取到buypet变量中 Scanner input = new Scanner(System.in); int buyPet = input.nextInt(); // switch判断用户输入数字 switch(buyPet){ case 1://当用户输入数字1,则宠物为小狗 pet = new Dog("旺财",2,100,"友爱","泰迪"); break; case 2://当用户输入数字2,则宠物为小企鹅 pet = new Penguin(); break; default: break; } return pet; } }
创建PetDemo测试类
//创建PetDemo测试类public class PetDemo { public static void main(String[] args) { // Dog dog = new Dog("旺财",2,100,"友爱","泰迪"); //dog.setName("旺财"); //dog.strain="泰迪"; //dog.setAge(2); //dog.setHealth(100); //dog.setLove("友爱"); // System.out.println(dog.getName()+"\t"+dog.strain+"\t"+dog.getAge()+"\t"+dog.getHealth()+"\t"+dog.getLove()); // dog.print(); // Penguin penguin = new Penguin(); // Pet pet1 = new Dog(); // Pet pet2 = new Penguin(); // pet1.shout(); // pet2.shout(); Petshop petshop = new Petshop(); // 调用子类方法重写父类 Pet pet3 = petshop.BuyPet(); // 实现宠物叫声的多态,抽象父类的引用指向子类的对象,实现多态 pet3.shout(); // if(pet3 instanceof Dog){// // Dog dog1 = (Dog)pet3;//// System.out.println(dog1.getStrain());// // }else{// // Penguin penguin1 = (Penguin)pet3;// // System.out.println(penguin1.getSex());// // }// // Pet dog = new Dog("旺财",12,100,"友爱","泰迪");// // dog.shout();// } }
结果验证一:
请输入您要购买的宠物:1.小狗 2.小企鹅
1
名叫旺财小狗汪汪的叫声!
结果验证二:
请输入您要购买的宠物:1.小狗 2.小企鹅
2
名叫null企鹅喵喵的叫声!
3、instanceof关键字
此关键字访问子类中特有的属性
public class PetDemo { public static void main(String[] args) { // Dog dog = new Dog("旺财",2,100,"友爱","泰迪"); //dog.setName("旺财"); //dog.strain="泰迪"; //dog.setAge(2); //dog.setHealth(100); //dog.setLove("友爱"); // System.out.println(dog.getName()+"\t"+dog.strain+"\t"+dog.getAge()+"\t"+dog.getHealth()+"\t"+dog.getLove()); // dog.print(); // Penguin penguin = new Penguin(); // Pet pet1 = new Dog(); // Pet pet2 = new Penguin(); // pet1.shout(); // pet2.shout(); Petshop petshop = new Petshop(); // 调用子类方法重写父类 Pet pet3 = petshop.BuyPet(); // 实现宠物叫声的多态,抽象父类的引用指向子类的对象,实现多态 pet3.shout(); if(pet3 instanceof Dog){ Dog dog1 = (Dog)pet3; System.out.println(dog1.getStrain()); }else{ Penguin penguin1 = (Penguin)pet3; System.out.println(penguin1.getSex()); } // Pet dog = new Dog("旺财",12,100,"友爱","泰迪");// // dog.shout();// } }
结果验证一:
请输入您要购买的宠物:1.小狗 2.小企鹅
1
名叫旺财小狗汪汪的叫声!
泰迪
结果验证二:
请输入您要购买的宠物:1.小狗 2.小企鹅
2
名叫null企鹅喵喵的叫声!
null