2023/7/05

发布时间 2023-07-05 18:40:26作者: 伐木工熊大

今天主要学习了包和内部类

一个完整的类名是包和类的结合,也就是:包.类的形式。一个包中是可以有多个不同的类的,同一包中的类相互访问时,可以不指定包名。

package 包;

public class Math {
    public static void main(String[] args)
    {
        Math a=new Math();
        //类的命名为:包+类名
        System.out.println("不是java.lang.Math类,而是包.Math类");
        System.out.println(a.getClass().getName());
    }
}

 

当一个包中的类要访问另一个包中的类时,就需要将另一个包导入:

import  包.*;访问导入包中所有的类

import  包.类;导入指定的类

还可以导入类中的静态成员

import 包.类.静态成员;

package 包;

public class Math {
    public static void main(String[] args)
    {
        Math a=new Math();
        //类的命名为:包+类名
        System.out.println("不是java.lang.Math类,而是包.Math类");
        System.out.println(a.getClass().getName());
    }
}
package 导入包;
import 包.*;//导入包中的所有类
import 包.Math;//导入包中的Math类
public class haihaihai {
    public static void main(String[] args)
    {
        Math.main(args);//能使用包中所有的类
    }
}
package 导入包;
import static java.lang.Math.max;//导入静态成员方法
import static java.lang.System.out;//导热油静态成员变量
public class hahaha {
    public static void main(String[] args) {
        out.println("521和1314的较大值为"+max(521,1314));//直接调用静态成员
    }

}

内部类就是类内部的类,它可以访问外部类中的所有私有成员,内部类的实例一定要在外部类的实例上,除非已经出现一个外部类对象,否则类中不会出现外部类对象。

成员内部类对象不止可以在外部类中使用,在其他类中也可以使用。

package 内部类;

public class car {
    private String brand;
    public car(String brand)
    {
        this.brand=brand;
    }
    class engine
    {
        String model;
        public engine(String model)
        {
            this.model=model;
        }
        public void show()
        {
            System.out.println(car.this.brand+this.model+"点火");//内部类可以直接访问外部类的私有数据
            //this在内部类中只能调用内部类的数据,而要调用外部类的数据得使用外部类名.this.的方法
        }
    }
    public void println()
    {
        System.out.println(brand+"创建成功");
    }
    public static void main(String[] args) {
        car a=new car("兰博基尼");
        a.println();
        //engine c=new engine("666");不能在主函数直接实例化
        car.engine b=a.new engine("EA211");
        b.show();
        /*
         * 内部类的实例一定要绑在外部类的实例上,不能单独存在
         * 创建内部类的方法得和外部类组合
         * 外部类 a=new 外部类();
         * 外部类.内部类 b=a.new 内部类();
         */
    }

}

最后时匿名内部类,这是一种没有类名的类,在类对象引用生成时定义,本质就像是抽象类的子类

package 正规军;
import static java.lang.System.out;
abstract class Dog
{
    String color;
    public abstract void move();
    public abstract void call();
}
public class hahah {
    public static void main(String[] args)
    {
        //Dog hello=new Dog();直接这样是会报错的,之前提到抽象类不能创建对象
        Dog hello=new Dog()//Dog抽象类的实现类没有类的名称,只有一个对象名,有点像Dog一个子类
                //匿名类不能写构造方法,不能定义静态的成员,没有对象的引用的话用完一次会被立刻销毁
        {
            public void move() {
                out.println("飞");
            }
            public void call()
            {
                out.println("焯");
            }
        };
        out.println(hello.getClass());//是一个 外部类名$序号.class 文件
        hello.color="五彩斑斓的黑";
        out.println(hello.color);
        hello.move();
        hello.call();
    }
}

最后是一些习题

package 内部类;
interface Moveable
{
    void run();
}
public class dog {
    public static void main(String[] args)
    {
        Moveable a=new Moveable()
                {
                    public void run()
                    {
                        System.out.println("小狗向前跑");
                    }
                };
                a.run();
    }
}
package 内部类;
abstract class fruit
{    
    
    abstract String getname();
}
public class human {
    String fruit;
    public human(String fruit)
    {
        this.fruit=fruit;
    }
    public static void main(String[] args)
    {
        human b=new human("苹果和香蕉");
        fruit a=new fruit()
                {
                    String getname()
                    {
                        return b.fruit ;
                    }
                };
        b.eat(a);
    }
    public void eat(fruit a)
    {
        System.out.println(a.getname());
    }
}
package 内部类;

public class people {
    class heart{
        public void show()
        {
        System.out.println("心脏执行");
        }
    }
    public void start()
    {
        System.out.println("人执行");
        heart a=new heart();//内部类可以在外部非静态方法类中直接实例化
        a.show();
    }
    public static void main(String args[])
    {
        people a=new people();
        a.start();
    }
}
package 内部类;
abstract class Animal
{
    public abstract void eat();
}
public class 吃 {

    public static void main(String[] args) {
        Animal a=new Animal() {
            public void eat()
            {
                System.out.println("猫吃鱼,狗吃肉");
            }
        };
        a.eat();
    }

}