首页 > 代码库 > java 一个文件中写多个class

java 一个文件中写多个class

一个java文件中可以包含多个类,但这些类中一个特殊类,这个类的属性为public,且这个类的名字与文件名相同,java文件中仅有一个类为public.

 

 

 interface Moveable {
    void run();
}

//具体产品角色
 class Plane implements Moveable {
    @Override
    public void run() {
        System.out.println("plane....");
    }
}

 class Broom implements Moveable {
    @Override
    public void run() {
        System.out.println("broom.....");
    }
}

//抽象工厂
abstract class VehicleFactory {
    abstract Moveable create();
}
//具体工厂
 class PlaneFactory extends VehicleFactory{
    public Moveable create() {
        return new Plane();
    }
}
 class BroomFactory extends VehicleFactory{
    public Moveable create() {
        return new Broom();
    }
}
//测试类
public class Main {
    public static void main(String[] args) {
        VehicleFactory factory = new BroomFactory();
        Moveable m = factory.create();
        m.run();
    }
}

 

java 一个文件中写多个class