zl程序教程

您现在的位置是:首页 >  Java

当前栏目

java进阶 -内部类 -静态类 -匿名类别类(重点)34

2023-04-18 15:47:27 时间

 

 

 

 静态类:

package com.cyjt97.interior;

public class day2 {
    public static void main(String[] args) {
        //        静态
        Outer.Inner.show();
//        Outer.Inner os = new Outer.Inner();
//        os.show();

    }
}
//静态内部类: static修饰的成员内部类
// 创建对象格式:外部类名.内部类名对象名=new外部类名.内部类对象();
class Outer{
    int a = 100;
    static int b =300;
    static class Inner{
//        要加static
        public static void show(){
            System.out.println("vs");
            Outer ss = new Outer();
            System.out.println(ss.a);
            System.out.println(b);
        }
    }
}

  匿名类:

 

 

 

 

 

不用匿名类方法:
package com.cyjt97.interior;

public class dai03 {
//    前提:需要存在一个接口或类
//    格式:
//    new 类名/接口 (){
//
//    }
    public static void main(String[] args) {
        useInter(new loser());
    }
//    问题:方法的形参是接口类型,我们该传入的是什么?
//    答案:传入的是该接口的实现类对象
    public static void useInter(lose i){
        i.show();
    }
}
interface lose{
   void show();
}
class loser implements lose{

    @Override
    public void show() {
        System.out.println("阿巴阿巴");
    }
}
package com.cyjt97.interior;

public class dai03 {
// 前提:需要存在一个接口或类
// 格式:
// new 类名/接口 (){
//
// }
// new 类名(){}: 代表继承这个类
// new 接口(){}:代表实现这个接口
// 优点:让代码简洁化,在定义类的时候对其实例化
// 抽象方法较少时使用

public static void main(String[] args) {
useInter(new loser());
useInter(new lose() {
@Override
public void show() {
System.out.println("你大爷的匿名类");
}
});
}
// 问题:方法的形参是接口类型,我们该传入的是什么?
// 答案:传入的是该接口的实现类对象
public static void useInter(lose i){
i.show();
}
}
interface lose{
void show();
}
class loser implements lose{

@Override
public void show() {
System.out.println("阿巴阿巴");
}
}