第一题:
1 /* 2 定义一个水果类(fruit),水果类中有 3 属性:颜色(color)、价格(price)、重量(weigth), 4 再定义一个<测试类>, 5 创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g。 6 然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g。 7 最后输出:苹果的颜色、价格、重量、 8 香蕉的颜色、价格、重量、 9 */ 10 package Homework; 11 public class Fruit 12 { 13 private String name; 14 15 private String colour; 16 private double price; 17 private String weight; 18 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public String getColour() { 26 return colour; 27 } 28 public void setColour(String colour) { 29 this.colour = colour; 30 } 31 public double getPrice() { 32 return price; 33 } 34 public void setPrice(double price) { 35 this.price = price; 36 } 37 public String getWeight() { 38 return weight; 39 } 40 public void setWeight(String weight) { 41 this.weight = weight; 42 } 43 44 public Fruit(String name,String colour, double price, String weight) { 45 super(); 46 this.name=name; 47 this.colour = colour; 48 this.price = price; 49 this.weight = weight; 50 } 51 public void fruit() 52 { 53 System.out.println(this.name+":"+this.colour+"、"+this.price+"元、"+this.weight); 54 } 55 56 57 }
1 package Homework; 2 3 public class TestFruit { 4 5 public static void main(String[] args) { 6 7 Fruit apple=new Fruit ("苹果","红色",5.5,"10g"); 8 apple.fruit(); 9 Fruit banana=new Fruit ("香蕉","黄色",4.2,"5g"); 10 banana.fruit(); 11 12 } 13 14 }
时间: 2024-10-05 05:11:39