1.抽象
共性,父类定义抽象方法,子类必须重写,或者子类也是抽象类
示例代码
animal
package animalabstract;
//定义抽象类animal
public abstract class animal {
String name;
int age;
//定义抽象方法eat,子类必须重写
public abstract void eat();
}
rabbit
package animalabstract;
public class rabbit extends animal{
@Override
public void eat() {
System.out.println("rabbit eat 胡萝卜");
}
}
frog
package animalabstract;
public class frog extends animal{
@Override
public void eat() {
System.out.println("frog eat 虫子");
}
}
dog
package animalabstract;
public class dog extends animal{
@Override
public void eat() {
System.out.println("dog eat 骨头");
}
}
Test
package animalabstract;
public class Test {
public static void main(String[] args) {
rabbit rb = new rabbit();
rb.eat();
frog fg = new frog();
fg.eat();
dog dog = new dog();
dog.eat();
}
}
抽象类里面可以有构造方法,比如
2.接口
示例代码
swim
package animalabstract;
public interface swim {
public void swim();
}
用上面抽象的示例代码,animal和rabbit不用改,但是dog类和frog类需要实现swim接口,需要改
dog
package animalabstract;
public class dog extends animal implements swim{
@Override
public void eat() {
System.out.println("dog eat 骨头");
}
@Override
public void swim() {
System.out.println("dog 狗刨");
}
}
frog
package animalabstract;
public class frog extends animal implements swim{
@Override
public void eat() {
System.out.println("frog eat 虫子");
}
@Override
public void swim() {
System.out.println("frog 蛙泳");
}
}
Test
package animalabstract;
public class Test {
public static void main(String[] args) {
rabbit rb = new rabbit();
rb.eat();
frog fg = new frog();
fg.eat();
fg.swim();
dog dog = new dog();
dog.eat();
dog.swim();
}
}
成员变量
只能是常量,默认 public static final
构造方法
没有
成员方法
抽象方法等, public abstract
3.接口,类之间的关系
实现多个接口里面有重名的方法也没事,因为是抽象的,需要重写
示例代码
代码结构
package coachplayer;
public abstract class person {
String name;
int age;
public person(String name, int age) {
this.name = name;
this.age = age;
}
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 abstract String info();
}
package coachplayer;
public abstract class player extends person{
public player(String name, int age) {
super(name, age);
}
public abstract String info();
public abstract void study();
}
package coachplayer;
public abstract class coach extends person{
public coach(String name, int age) {
super(name, age);
}
public abstract String info();
public abstract void teach();
}
package coachplayer;
public class ppplayer extends player implements english {
public ppplayer(String name, int age) {
super(name, age);
}
@Override
public String info() {
return this.name + " " + this.age ;
}
@Override
public void study() {
System.out.println("乒乓球运动员学习打乒乓球");
}
@Override
public void english() {
System.out.println("乒乓球运动员说英语");
}
}
package coachplayer;
public class bbplayer extends player{
public bbplayer(String name, int age) {
super(name, age);
}
@Override
public String info() {
return this.name + " " + this.age ;
}
@Override
public void study() {
System.out.println("篮球运动员打篮球");
}
}
package coachplayer;
public class ppcoach extends coach implements english{
public ppcoach(String name, int age) {
super(name, age);
}
@Override
public String info() {
return this.name + " " + this.age ;
}
@Override
public void teach() {
System.out.println("乒乓球教练教乒乓球");
}
@Override
public void english() {
System.out.println("乒乓球教练说英语");
}
}
package coachplayer;
public class bbcoach extends coach {
public bbcoach(String name, int age) {
super(name, age);
}
@Override
public String info() {
return this.name + " " + this.age ;
}
@Override
public void teach() {
System.out.println("篮球教练教篮球");
}
}
package coachplayer;
public interface english {
public void english();
}
package coachplayer;
public class Test {
public static void main(String[] args) {
ppplayer ppl=new ppplayer("leo",18);
ppl.study();
ppl.english();
System.out.println(ppl.info());
bbplayer bbl=new bbplayer("jack",20);
bbl.study();
System.out.println(bbl.info());
ppcoach ppc=new ppcoach("james",50);
ppc.teach();
ppc.english();
System.out.println(ppc.info());
bbcoach bbc=new bbcoach("amy",40);
bbc.teach();
System.out.println(bbc.info());
}
}
4.接口特殊方法
default
在接口里定义 fun,默认 public abstract ,所以在实现这个接口的时候,就必须对该方法重写
package interface3tips;
public interface inter0 {
public abstract void fun();
}
package interface3tips;
public class class1 implements inter0{
@Override
public void fun() {
System.out.println("class继承了inter0");
}
}
package interface3tips;
public class TEST {
public static void main(String[] args) {
class1 c1 = new class1();
c1.fun();
}
}
但是,把abstract改成default,实现这个接口的时候就不需要重写,因为他是默认的方法,你可以重写也可以不重写,然后可以直接使用
package interface3tips;
public interface inter0 {
public default void fun(){
System.out.println("fun的default");
}
}
package interface3tips;
public class class1 implements inter0{
}
package interface3tips;
public class TEST {
public static void main(String[] args) {
class1 c1 = new class1();
c1.fun();
}
}
static
package interface3tips;
public interface inter0 {
public default void fun(){
System.out.println("fun的default");
}
public static void show(){
System.out.println("inter0的静态方法");
}
}
package interface3tips;
public class class1 implements inter0{
}
package interface3tips;
public class TEST {
public static void main(String[] args) {
class1 c1 = new class1();
c1.fun();
inter0.show();
}
}
private
package interface3tips;
public interface inter0 {
public default void fun(){
System.out.println("fun的default");
fun1();
}
public static void show(){
System.out.println("inter0的静态方法");
}
private void fun1(){
System.out.println("私有方法,外类访问不到");
}
}
‘
package interface3tips;
public class class1 implements inter0{
}
package interface3tips;
public class TEST {
public static void main(String[] args) {
class1 c1 = new class1();
c1.fun();
inter0.show();
}
}
package interface3tips;
public interface inter0 {
public default void fun(){
System.out.println("fun的default");
fun1();
}
public static void show(){
System.out.println("inter0的静态方法");
fun2();
}
private void fun1(){
System.out.println("私有方法,外类访问不到");
}
private static void fun2(){
System.out.println("私有静态方法,外类访问不到");
}
}
package interface3tips;
public class class1 implements inter0{
}
package interface3tips;
public class TEST {
public static void main(String[] args) {
class1 c1 = new class1();
c1.fun();
inter0.show();
}
}
5.适配器
当接口inter0有很多个抽象方法,那么一个类class1继承的时候就必须重写所有的抽象方法,但我不想这样,所以先创建一个新的类作为适配器inter0Adapter,实现这个接口,然后再用类来继承这个适配器类 class1 extends interAdapter,这样我的class1类就不用重写inter0的所有抽象方法
package interface3tips;
public interface inter0 {
public abstract void fun1();
public abstract void fun2();
public abstract void fun3();
public abstract void fun4();
public abstract void fun5();
}
package interface3tips;
public abstract class inter0Adapter implements inter0{
@Override
public void fun1() {
System.out.println("适配器的fun1");
}
@Override
public void fun2() {
System.out.println("适配器的fun2");
}
@Override
public void fun3() {
System.out.println("适配器的fun3");
}
@Override
public void fun4() {
System.out.println("适配器的fun4");
}
@Override
public void fun5() {
System.out.println("适配器的fun5");
}
}
package interface3tips;
public class class1 extends inter0Adapter{
@Override
public void fun5(){
System.out.println("只用到fun5");
}
}
package interface3tips;
public class TEST {
public static void main(String[] args) {
class1 c1 = new class1();
c1.fun1();
c1.fun5();
}
}
6.内部类
1.成员内部类
package Inclass;
public class Outer{
String name;
Iner I1;
public Outer(String name) {
this.name = name;
}
public Outer() {
}
class Iner{
int age;
public Iner() {
}
public Iner(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Iner{" +
"age=" + age +
'}';
}
}
public Iner getI1() {
if (I1 == null) {
I1 = new Iner(0); // 默认年龄为 0
}
return I1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Outer{" +
"name='" + name + '\'' +
", I1=" + I1.toString() +
'}';
}
public void setI1(Iner i1) {
I1 = i1;
}
}
package Inclass;
public class Test {
public static void main(String[] args) {
// 方法1,直接创建
Outer Outer1=new Outer("jack");
Outer.Iner Iner1=Outer1.new Iner(18);
Outer1.setI1(Iner1);
System.out.println(Outer1);
// 方法2,外部类编写方法返回内部类n对象
Outer Outer2=new Outer("james");
Outer.Iner Iner2=Outer2.getI1();
Iner2.setAge(20);
System.out.println(Outer2);
}
}
2.静态内部类
3.局部内部类
4.匿名内部类
匿名内部类是一个对象来的,只是没有名字,只用一次,就不用在创建一个类了
package Inclass;
public abstract class Outer{
String name;
public Outer(String name) {
this.name = name;
}
public Outer() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Outer{" +
"name='" + name + '\'' +
'}';
}
public abstract void show();
}
package Inclass;
public class Test {
public static void main(String[] args) {
new Outer() {
public void show () {
System.out.println("我是匿名内部类");
}
}.show();
}
}
也可以创建一个对象接收,然后再用
package Inclass;
public class Test {
public static void main(String[] args) {
Outer o1= new Outer() {
public void show () {
System.out.println("我是匿名内部类");
}
};
o1.show();
}
}