//Animal.java package com.ljsblog.domain2; public abstract class Animal{ private String name; private int age; public Animal(){} public Animal(String name,int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } public abstract void eat(); public void drink(){ System.out.println("喝水"); } }
//Frog.java package com.ljsblog.domain2; public class Frog extends Animal { public Frog(){} public Frog(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("吃虫子"); } }
//Dog.java package com.ljsblog.domain2; public class Dog extends Animal{ public Dog(){} public Dog(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("吃骨头"); } }
//Sheep.java package com.ljsblog.domain2; public class Sheep extends Animal{ public Sheep(){} public Sheep(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("吃草"); } }
//Test.java package com.ljsblog.domain2; public class test { public static void main(String[] args) { Dog g = new Dog("傻狗", 18); Frog f = new Frog("癞蛤蟆", 20); Sheep s = new Sheep("喜洋洋", 3); g.eat(); g.drink(); f.eat(); f.drink(); s.eat(); s.drink(); } } /* 吃骨头 喝水 吃虫子 喝水 吃草 喝水 */
接口
接口就是一种规则,是对行为的抽象。
定义和使用
接口用关键字interface定义:
public interface 接口名{}
接口不能实例化。
接口与类之间是实现关系,通过implements关键字表示:
public class 类名 implements 接口名{}
接口的子类(实现类):
要么重写接口类中的所有抽象方法
要么是抽象类
注意事项
接口与类可以单实现,也可以多实现
public class 类名 implements 接口名1,接口名2…{}
实现类可以在继承一个类的同时实现多个接口
public class 类名 extends 父类名 implements 接口名1,接口名2…{}
//父类 //Animal.java package com.ljsblog.domain2; public abstract class Animal{ private String name; private int age; public Animal(){} public Animal(String name,int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } public abstract void eat(); }
1 2 3 4 5 6
//接口 //Regularity package com.ljsblog.domain2; public interface Regularity{ public abstract void swim(); }
//子类 //Frog.java package com.ljsblog.domain2; public class Frog extends Animal implements Regularity { public Frog(){} public Frog(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("吃虫子"); } @Override public void swim(){ System.out.println("蛙泳"); } }
//Dog.java package com.ljsblog.domain2; public class Dog extends Animal implements Regularity{ public Dog(){} public Dog(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("吃骨头"); } @Override public void swim(){ System.out.println("狗刨"); } }
//Rabbit.java package com.ljsblog.domain2; public class Rabbit extends Animal{ public Rabbit(){} public Rabbit(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("吃胡萝卜"); } }
//Person.java package com.ljsblog.domain3; public class Test { public static void main(String[] args){ PingAthlete pa=new PingAthlete("张三",16); pa.study(); pa.speakEnglish(); BasketballAthele ba=new BasketballAthele("李四",16); ba.study(); PingTeacher pt=new PingTeacher("王五一",26); pt.teach(); pt.speakEnglish(); BasketBallTeacher bt=new BasketBallTeacher("王五二",26); bt.teach(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//Athlete.java package com.ljsblog.domain3; public abstract class Athlete extends Person{ public Athlete(){} public Athlete(String name,int age){ super(name,age); } public abstract void study(); }
//Teacher.java package com.ljsblog.domain3; public abstract class Teacher extends Person{ public Teacher(){} public Teacher(String name,int age){ super(name,age); } public abstract void teach(); }
//Speak.java package com.ljsblog.domain3; public interface Speak{ public abstract void speakEnglish(); }
//BasketballAthele.java package com.ljsblog.domain3; public class BasketballAthele extends Athlete{ public BasketballAthele(){} public BasketballAthele(String name,int age){ super(name,age); } @Override public void study(){ System.out.println("年龄为"+getAge()+"的篮球运动员"+getName()+"学打篮球"); } }
//BasketBallTeacher.java package com.ljsblog.domain3; public class BasketBallTeacher extends Teacher{ public BasketBallTeacher(){} public BasketBallTeacher(String name,int age){ super(name,age); } @Override public void teach(){ System.out.println("年龄为"+getAge()+"的篮球教练"+getName()+"教打篮球"); } }
//PingAthlete.java package com.ljsblog.domain3; public class PingAthlete extends Athlete implements Speak{ public PingAthlete(){} public PingAthlete(String name,int age){ super(name,age); } @Override public void study(){ System.out.println("年龄为"+getAge()+"的乒乓球运动员"+getName()+"学打乒乓球"); } @Override public void speakEnglish(){ System.out.println("乒乓球运动员在说英语"); } }
//PingTeacher.java package com.ljsblog.domain3; public class PingTeacher extends Teacher implements Speak{ public PingTeacher(){} public PingTeacher(String name,int age){ super(name,age); } @Override public void teach(){ System.out.println("年龄为"+getAge()+"的乒乓球教练"+getName()+"教打乒乓球"); } @Override public void speakEnglish(){ System.out.println("乒乓球教练在说英语"); } }
//接口 public interface Inter{ public abstract void show1(); public abstract void show2(); public abstract void show3(); public abstract void show4(); public abstract void show5(); public abstract void show6(); }
package com.ljsblog.domain5; public class Outer{ private int a=1; class Inner{ private int a=2; public void show(){ int a=3; System.out.println(Outer.this.a);//1 System.out.println(this.a);//2 System.out.println(a);//3 } } }
1 2 3 4 5 6 7
package com.ljsblog.domain5; public class Test{ public static void main(String[] args){ Outer.Inner oi=new Outer().new Inner(); oi.show(); } }