1、两个文件:Fruit.java和Apple.java
Fruit.java:
package lee;
public class Fruit
{
public double weight;
public void info(){
System.out.println("I am a Fruit ! weight " + weight + "g!");
}
}
Apple.java:
package lee;
public class Apple extends Fruit
{
public static void main(String[] args)
{
Apple a = new Apple();
a.weight = 50;
a.info();
}
}
2、使用javac进行编译
因为Apple继承了Fruit,所以需要同时编译子类和父类:
命令如下:
javac -d E:\Dev_Learn\Java_Project\classes Apple.java Fruit.java