java 类的继承和接口的继承
父类 public class person { String name; int age; void eat(){ System.out.println("吃饭"); } void introduce(){ System.out.println("我的名字是"+name +",我的年龄是"+age); } }
子类 public class testper extends person { int grade; void study(){ System.out.println("学习,年级是"+this.grade); } }
测试下继承 public class ss { public static void main(String[] args){ testper testper=new testper(); testper.grade=1; testper.name="北京"; testper.age=10; testper.eat(); testper.introduce(); testper.study(); } }
输出
java的继承只能是单继承。
java接口的继承
usb接口:
interface Useb { void read(); void write(); }
wifi接口
public interface WiFi { public void open(); public void close(); }
实现 public class Usebpone implements Useb,WiFi{ public void read() { System.out.println("useb read"); } public void write() { System.out.println("useb write"); } public void open(){ System.out.println("wifi open"); } public void close() { System.out.println("wifi open"); } }
测试 public class test { public static void main(String agrs[]) { Usebpone usbPhone=new Usebpone(); Useb sab=usbPhone; sab.read(); sab.write(); WiFi wiFi=usbPhone; wiFi.open(); wiFi.close(); } }
运行结果