day 2 java

抽象类
只有抽象类才能定义抽象方法
在抽象类中可以定义普通方法,普通变量
子类继承抽象类 子类需是abstract类或者子类必须重写父类的抽象方法
抽象方法没有方法体

eg

public abstract class Anmial {
public abstract void say();//没有方法体

}

class Bird extends Anmial{

public void say() {

	System.out.println("渣渣渣");
}
}

抽象类不能实例化(Animal a =new Animal())错但是可以 Animal a =new Bird() ;
public static void main(String[] args) {
Anmial a=new Bird(); 此时会打印出 创建一个动物 但就Animal a;没有输出

}

}

回调(callback)
Hook 钩子
模版方法模式
eg
public class Panitdraw {

public  static  void  draw(A f){
	System.out.println("N");
	System.out.println("m");
	System.out.println("s");
      f.paint();
      System.out.println("L");
}

public static void main(String[] args) {
	B b =new B();
	draw(b)	;
	}

}

   class A{
	public void paint() {
		System.out.println("A");
	}
	
}

 
  class B extends A{
	 public void paint() {
			System.out.println("B");
		}
 }
  class C extends A{
	 public void paint() {
			System.out.println("C");
		}
 }

N
M
S
L
B

内部类
import Objec.Face.Nose;

public class Outer {
public static void main(String[] args) {
Face f=new Face(); //不能直接调用 Nose n=new Nose();
Nose n = f.new Nose(); // 也可写成 Face。Nose n=f。new Nose
n.breath();
Face.Ear ear=new Face.Ear();
ear.say();
}
}

class Face{      
	int type;     // 非静态内部类 不能定义静态变量 静态方法
	String a="FFF";
static	String b="CCCC"	;	
	class Nose{
	String type;
	 void breath() {
		 System.out.println( Face.this.type);// e 非静态内部类可以使用外部类的成员 反过来不行
		                             
	 }
	 
	}
	static  class Ear   {  //(这是一个静态内部类)有静态内部类实例不一定有外部类实例
    void say(){
    	//System.out.println(a);会报错 静态内部类不能直接访问外部类的非静态变量可以访问外部类的静态变量
    	System.out.println(b);
    }
	
	}
}

输出
0
CCCC

猜你喜欢

转载自blog.csdn.net/qq_40604035/article/details/88542095