多态看一下运行结果
代码如下:
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("---------------------------------");
接口
运行结果
package interfacea; public class aer implements USBA{ @Override public void turnOn() { // TODO Auto-generated method stub System.out.println("are已启动,请开始以下程序"); } @Override public void turnruning() { // TODO Auto-generated method stub System.out.println("are开始运行自己设定的程序"); } @Override public void turnOff() { // TODO Auto-generated method stub System.out.println("手机结束自己运行的程序系统"); } @Override public void name() { // TODO Auto-generated method stub System.out.println("are名称为huaweiwww"); } @Override public void color() { // TODO Auto-generated method stub System.out.println("are色彩为back"); } @Override public void height() { // TODO Auto-generated method stub System.out.println("are高度为8cm"); } @Override public void weight() { // TODO Auto-generated method stub System.out.println("are宽度为15cm"); } @Override public void length() { // TODO Auto-generated method stub System.out.println("are长度为20cm"); } @Override public void m() { // TODO Auto-generated method stub System.out.println("m有飒飒"); } }
package interfacea; public class ElectronicWhiteboard implements USBA{ @Override public void turnOn() { // TODO Auto-generated method stub System.out.println("电子白板真在运行,打开你要的软件,并开始运行"); } @Override public void turnruning() { // TODO Auto-generated method stub System.out.println("电子白板真在运行,正在运行这个程序,并开始运行"); } @Override public void turnOff() { // TODO Auto-generated method stub System.out.println("电子白板结束自己运行的程序系统"); } @Override public void name() { // TODO Auto-generated method stub System.out.println("电子白板名称为huaweiwww"); } @Override public void color() { // TODO Auto-generated method stub System.out.println("电子白板色彩为back"); } @Override public void height() { // TODO Auto-generated method stub System.out.println("电子白板高度为150cm"); } @Override public void weight() { // TODO Auto-generated method stub System.out.println("电子白板宽度为200cm"); } @Override public void length() { // TODO Auto-generated method stub System.out.println("电子白板长度为300cm"); } @Override public void m() { // TODO Auto-generated method stub System.out.println("m有飒飒"); } }
package interfacea; public class Keyboarda implements USBA { @Override public void turnOn() { // TODO Auto-generated method stub System.out.println("键盘已启动,请开始以下程序 ok"); } @Override public void turnruning() { // TODO Auto-generated method stub System.out.println("键盘开始运行自己设定的程序"); } @Override public void turnOff() { // TODO Auto-generated method stub System.out.println("键盘结束自己运行的程序系统"); } @Override public void name() { // TODO Auto-generated method stub System.out.println("键盘名称为huaweiwww"); } @Override public void color() { // TODO Auto-generated method stub System.out.println("键盘色彩为back"); } @Override public void height() { // TODO Auto-generated method stub System.out.println("键盘高度为10cm"); } @Override public void weight() { // TODO Auto-generated method stub System.out.println("键盘宽度为30cm"); } @Override public void length() { // TODO Auto-generated method stub System.out.println("键盘长度为80cm"); } @Override public void m() { // TODO Auto-generated method stub System.out.println("m有飒飒"); } }
package interfacea; public class Mousea implements USBA { @Override public void turnOn() { // TODO Auto-generated method stub System.out.println("鼠标已启动,请开始以下程序"); } @Override public void turnruning() { // TODO Auto-generated method stub System.out.println("鼠标开始运行自己设定的程序"); } @Override public void turnOff() { // TODO Auto-generated method stub System.out.println("鼠标结束自己运行的程序系统"); } @Override public void name() { // TODO Auto-generated method stub System.out.println("鼠标名称为huaweiwwwe"); } @Override public void color() { // TODO Auto-generated method stub System.out.println("鼠标色彩为back"); } @Override public void height() { // TODO Auto-generated method stub System.out.println("鼠标高度为50mm"); } @Override public void weight() { // TODO Auto-generated method stub System.out.println("鼠标宽度为500mm"); } @Override public void length() { // TODO Auto-generated method stub System.out.println("鼠标长度为900mm"); } @Override public void m() { // TODO Auto-generated method stub System.out.println("m有飒飒"); } }
package interfacea; public class Phonea implements USBA{ @Override public void turnOn() { // TODO Auto-generated method stub System.out.println("手机已启动,请开始以下程序"); } @Override public void turnruning() { // TODO Auto-generated method stub System.out.println("手机开始运行自己设定的程序"); } @Override public void turnOff() { // TODO Auto-generated method stub System.out.println("手机结束自己运行的程序系统"); } @Override public void name() { // TODO Auto-generated method stub System.out.println("手机名称为huaweiwww"); } @Override public void color() { // TODO Auto-generated method stub System.out.println("手机色彩为back"); } @Override public void height() { // TODO Auto-generated method stub System.out.println("手机高度为8cm"); } @Override public void weight() { // TODO Auto-generated method stub System.out.println("手机宽度为15cm"); } @Override public void length() { // TODO Auto-generated method stub System.out.println("手机长度为20cm"); } @Override public void m() { // TODO Auto-generated method stub System.out.println("m有飒飒"); } }
package interfacea; public class telephone implements USBA { @Override public void turnOn() { // System.out.println("电话已启动,请打电话"); } @Override public void turnruning() { // TODO Auto-generated method stub System.out.println("电话开始进行联系对方"); } @Override public void turnOff() { // TODO Auto-generated method stub System.out.println("电话结束:请把电源turnoff"); } @Override public void name() { // TODO Auto-generated method stub System.out.println("电话名称为大哥大"); } @Override public void color() { // TODO Auto-generated method stub System.out.println("电话色彩为开门红"); } @Override public void height() { // TODO Auto-generated method stub System.out.println("电话高度为6cm"); } @Override public void weight() { // TODO Auto-generated method stub System.out.println("电话宽度为12cm"); } @Override public void length() { // TODO Auto-generated method stub System.out.println("电话长度为18cm"); } @Override public void m() { // TODO Auto-generated method stub System.out.println("m有飒飒"); } }
package interfacea; import java.util.ArrayList; public class DwmoTesta { public static void main(String[] args) { ArrayList <USBA>usbs =new <USBA> ArrayList (); usbs.add( new Mousea()); //类 usbs.add(new Keyboarda()); usbs.add(new Phonea()); usbs.add(new ElectronicWhiteboard ()); usbs.add(new telephone ()); usbs.add(new aer()); usbs.add(new HuBING()); System.out.println("用for循化输出内容"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).height(); } System.out.println("height"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).weight(); } System.out.println("weight"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).length(); } System.out.println("设定设备的长:宽:高"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).turnOn(); //接口方法 } System.out.println("设定程序启动的系统"); System.out.println("============================"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).turnOff(); } System.out.println("设置程序正在运行的程序"); System.out.println("============================"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).turnruning(); } System.out.println("设置程序名称"); System.out.println("**********************"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).name(); } System.out.println("设置设备色彩为"); System.out.println("**********************"); for (int i = 0; i < usbs.size(); i++) { usbs.get(i).color(); } for (int i = 0; i < usbs.size(); i++) { usbs.get(i).m(); } } }