java笔记-内部类
四种:非静态内部类、静态内部类、匿名类、本地类
非静态内部类
非静态内部类可以直接在一个类里面定义
非静态内部类 只有在一个对象存在的时候才有意义,所以实例化Score 的时候,必须建立在一个存在的类的基础上
语法: new 外部类().new 内部类()
作为Student的非静态内部类,是可以直接访问外部类的private实例属性name的
public class Student{
private String name; // 姓名
class Score {
int score;
public void ifScore() {
if (score>= 8)
System.out.println(name + "优秀");
else
System.out.println(name + "一般");
}
}
public static void main(String[] args) {
Student stu= new Student();
stu.name = "小明";
// 实例化内部类
// BattleScore对象只有在一个英雄对象存在的时候才有意义
// 所以其实例化必须建立在一个外部类对象的基础之上
ifScore score = stu.new ifScore();
score.score= 9;
score.ifScore();
}
}
静态内部类
在一个类里面声明一个静态内部类,与非静态内部类不同,静态内部类的实例化 不需要一个外部类的实例为基础,可以直接实例化
语法:new 外部类.静态内部类();
因为没有一个外部类的实例,所以在静态内部类里面不可以访问外部类的实例属性和方法
除了可以访问外部类的私有静态成员外,静态内部类和普通类没什么大的区别
public class Student{
public String name;
protected float core;
private static void test(){
System.out.println("考试结束了");
}
//成绩
static class Score{
int core=80;
//如果core大于60,则为优秀
public void checkScore(){
if(core>=0){
Student.test();
//静态内部类不能直接访问外部类的对象属性
System.out.println(name + "完成了这个考试");
}
}
}
public static void main(String[] args) {
//实例化静态内部类
Student.Score crystal = new Student.Score ();
crystal.checkScore();
}
}
匿名类
匿名类指的是在声明一个类的同时实例化它,使代码更加简洁精练
通常情况下,要使用一个接口或者抽象类,都必须创建一个子类
有的时候,为了快速使用,直接实例化一个抽象类,并“当场”实现其抽象方法。
既然实现了抽象方法,那么就是一个新的类,只是这个类,没有命名。
这样的类,叫做匿名类
public abstract class Student{
String name;
int id;
float core;
public abstract void test();
public static void main(String[] args) {
StudentScore adh=new StudentScore ();
//通过打印adh,可以看到adh这个对象属于StudentScore 类
adh.test();
System.out.println(adh);
//---------------------
Student h = new Student(){
//当场实现test方法
public void test() {
System.out.println("新的测试");
}
};
//---------------------
h.attack();
//通过打印h,可以看到h这个对象属于Hero$1这么一个系统自动分配的类名
System.out.println(h);
}
}
接口
import Abstract.Hero;
// 类:NewInterface.StudentScore
// 继承了Student类,所以继承了name,id,core等属性
// 实现某个接口,就相当于承诺了某种约定
//
// 所以,实现了Score这个接口,就必须提供Score接口中声明的方法physicTest()
// 实现在语法上使用关键字 implements
public class StudentScore extends Student implements Score{
// 当一个方法带有@override表示该方法是重写父类的方法
@Override
public void physicTest() {
System.out.println("进行物理test");
}
@Override
public void test() {
physicTest();
}
}
本地类
内部类与匿名类不一样的是,内部类必须声明在成员的位置,即与属性和方法平等的位置。
本地类和匿名类一样,直接声明在代码块里面,可以是主方法,for循环里等等地方
public abstract class Studeent{
String name;
int id;
float core;
public abstract void test();
public static void main(String[] args) {
-----------------------
//与匿名类的区别在于,本地类有了自定义的类名
class SomeStudent extends Student{
public void test() {
System.out.println( name+ " 新的测试");
}
}
---------------------------
SomeStudent h =new SomeStudent();
h.name ="地卜师";
h.attack();
}
}
接口
import Abstract.Hero;
// 类:NewInterface.StudentScore
// 继承了Student类,所以继承了name,id,core等属性
// 实现某个接口,就相当于承诺了某种约定
//
// 所以,实现了Score这个接口,就必须提供Score接口中声明的方法physicTest()
// 实现在语法上使用关键字 implements
public class StudentScore extends Student implements Score{
// 当一个方法带有@override表示该方法是重写父类的方法
@Override
public void physicTest() {
System.out.println("进行物理test");
}
@Override
public void test() {
physicTest();
}
}