package?com.Painter.Demo1;
成都創(chuàng)新互聯(lián)公司專注于高安企業(yè)網站建設,響應式網站,商城網站開發(fā)。高安網站建設公司,為高安等地區(qū)提供建站服務。全流程定制開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務
public?class?AbstractDemo?{
//?用java?語言編程實現(xiàn)定義抽象水果類,定義其子類實現(xiàn)其抽象的方法。
public?static?void?main(String[]?args)?
{
Fruits?apple?=?new?Apple();
Fruits?orange?=?new?Orange();
apple.getFruits();
orange.getFruits();
}
}
abstract?class?Fruits
{
public?abstract?void?getFruits();
}
class?Apple?extends?Fruits
{
public?void?getFruits()
{
System.out.println("蘋果");
}
}
class?Orange?extends?Fruits
{
public?void?getFruits()
{
System.out.println("橘子");
}
}
/**
* 1.定義一個水果類Fruit,符合如下要求:
(a) 類Fruit的成員變量;
weight表示水果的質量,數(shù)據類型為float
color表示水果的顏色,數(shù)據類型為string.
(b)類Fruit的成員方法:
getWeight()獲得水果的質量
getColor()獲得水果的顏色
2、按照第1題中的水果類Fruit的定義,創(chuàng)建該類的對象f1和f2,其中f1的weight值為0.86, color值為yellow;
f2的weight值為0.73, color值為red; 存儲并輸出f1和f2的值,計算f1和f2的平均值。
* @author Administrator
*
*/
public class Fruit {
private float weight;
private String color;
//構造函數(shù):用于存儲對象的2個值
public Fruit(float weight, String color){
this.weight = weight;
this.color = color;
}
public float getWeight() {
return this.weight;
}
public String getColor() {
return this.color;
}
//求重量的平均值
public static float avg(float w1, float w2){
return w1*w2/2;
}
public static void main(String[] args){
Fruit f1 = new Fruit((float) 0.86, "yellow");
Fruit f2 = new Fruit((float) 0.73, "red");
System.out.println("f1:" + "\n" + " weitht:" + f1.getWeight() + "\n" + " color:" + f1.getColor());
System.out.println("f2:" + "\n" + " weitht:" + f2.getWeight() + "\n" + " color:" + f2.getColor());
System.out.println("平均值:" + Fruit.avg(f1.getWeight(), f2.getWeight()));
}
}
這么簡單的題目也不會,你學了點啥?
class Fruit
{
public Fruit()
{
System.out.println("Fruit類的構造方法。");
}
}
class Apple extends Fruit
{
public Apple()
{
super();
System.out.println("Apple類的構造方法。");
}
}
弄了一下,代碼如下,你可以參考參考:
Fruit類:
public?abstract?class?Fruit?{
private?String?shape;
public?String?getShape()?{
return?shape;
}
public?void?setShape(String?shape)?{
this.shape?=?shape;
}
public?abstract?void?eat();
}
Apple類:
public?class?Apple?extends?Fruit?{
public?Apple(String?shape)?{
setFatherShape(shape);
}
public?void?setFatherShape(String?shape)
{
??super.setShape(shape);
}
public?String?getFatherShape()
{
return?super.getShape();
}
@Override
public?void?eat()
{
System.out.println(getFatherShape()+"的蘋果好甜");
}
}
Banana類:
public?class?Banana?extends?Fruit?{
public?Banana(String?shape)?{
setFatherShape(shape);
}
public?void?setFatherShape(String?shape)
{
??super.setShape(shape);
}
public?String?getFatherShape()
{
return?super.getShape();
}
@Override
public?void?eat()
{
System.out.println(getFatherShape()+"的香蕉好香");
}
}
Orange類:
public?class?Orange?extends?Fruit?{
public?Orange(String?shape)?{
setFatherShape(shape);
}
public?void?setFatherShape(String?shape)
{
??super.setShape(shape);
}
public?String?getFatherShape()
{
return?super.getShape();
}
@Override
public?void?eat()
{
System.out.println(getFatherShape()+"的桔子好酸");
}
}
Game類:
public?class?Game?{
public??Fruit?luckDraw(){
Random?random=new?Random();
int?luckNum=?random.nextInt(3);//隨機產生一個0-2之間的數(shù)
Fruit?fruit?=?null;
//0-蘋果(圓圓的)、1-香蕉(彎彎的)、2-桔子(長長的)
if(luckNum==0){
fruit=new?Apple("圓圓的");
}else?if?(luckNum==1)?{
fruit=new?Banana("彎彎的");
}else?if?(luckNum==2)?{
fruit=new?Orange("長長的");
}
return?fruit;
}
public?static?void?main(String[]?args)?{
Fruit[]?fruits=new?Fruit[10];
Game?game=new?Game();
for?(int?i=0;i10;i++)?{
fruits[i]=game.luckDraw();
fruits[i].eat();
}
}
}
運行結果:
彎彎的的香蕉好香
彎彎的的香蕉好香
長長的的桔子好酸
長長的的桔子好酸
圓圓的的蘋果好甜
長長的的桔子好酸
圓圓的的蘋果好甜
彎彎的的香蕉好香
長長的的桔子好酸
長長的的桔子好酸
樓主若覺得回答有所幫助,望采納,謝謝!