Java抽象类和接口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java抽象类和接口相关的知识,希望对你有一定的参考价值。
1、定义Pet抽象类
public abstract 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方法、set方法 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; } public void setHealth(int health) { this.health = health; } public String getLove() { return love; } public void setLove(String love) { this.love = love; } //父类的打印方法 public void print(){ System.out.println(name+"\t"+age+"\t"+health+"\t"+love); } //抽象方法:访问修饰符+abstract关键字+返回值+方法名 //抽象方法必须在抽象类中,抽象方法没有方法体(没有大括号) public abstract void shout(); //普通方法 /*public void shout(){ System.out.println("宠物的叫声!"); }*/ } //Dog子类 public class Dog extends Pet { public String strain; // public Dog() { // // //System.out.println("子类无参构造方法"); // } public Dog(String name, int age, int health, String love,String strain) { super(name, age, health, love); //System.out.println("子类有参构造方法"); } // public String getStrain() { // return strain; // } // // public void setStrain(String strain) { // this.strain = strain; // } //子类方法重写 // public void print(){ // // System.out.println(super.getName()+"\t"+super.getAge()+"\t"+super.getHealth()+"\t"+super.getLove()+"\t"+strain); // } public void shout(){ System.out.println("名叫"+this.getName()+"的小狗汪汪的叫!"); } } //Pet测试类 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、//定义电源插座的接口,具有供电的能力;
public interface Socket { public void getPower(); }
//定义类:电视类实现电源插座接口;
public class TV implements Socket{ public void getPower(){ System.out.println("电视机插入电源插座开始播放电视节目。"); } }
//定义测试类,包含电器开始工作的方法,参数为电器对象;
public class SocketDemo { public static void main(String[] args) { Socket tv = new TV(); tv.getPower(); } }
//测试结果
电视机插入电源插座开始播放电视节目。
3、
//
public abstract class Person_student { public String name; public Person_student(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public interface Work { public void getWork(); }
public interface Study { public void getStudy(); }
public class Graduate_student extends Person_student implements Work,Study { public Graduate_student(String name) { super(name); } public void getWork(){ System.out.println(this.getName()+"研究生具有工作能力。"); } public void getStudy(){ System.out.println(this.getName()+"研究生具有学习能力。"); } }
public class GraduateDemo { public static void main(String[] args) { Graduate_student stu = new Graduate_student("张三"); stu.getStudy(); stu.getWork(); } }
本文出自 “技术进阶” 博客,请务必保留此出处http://vipnoon.blog.51cto.com/7589908/1795087
以上是关于Java抽象类和接口的主要内容,如果未能解决你的问题,请参考以下文章