package food;
public abstract class Fruit{
protected String color;
protected String name;
public void setColor(String str){
color = str;
}
public abstract void show();
}
Apple.java
import food.Fruit;
class Apple extends Fruit{
protected int price;
public Apple(){}
public Apple(int price){
this.price = price;
}
public void setName(String name){
this.name = name;
}
public void setColor(String color){
this.color = color;
}
public void show(){
System.out.println("There is a fruit, it's color is " + color + ",it's an " + name + ".");
System.out.println("And the price of an apple is " + price + ".");
}
public static void main(String[] args){
Apple apple = new Apple(10);
apple.setName("Apple");
apple.setColor("red");
apple.show();
}
}
-------------------------------------------------------------------------
$ java -classpath /Users/superppp/Documents/java Apple
There is a fruit, it's color is red,it's an Apple.
And the price of an apple is 10.
沒有留言:
張貼留言