Homework (7th,Mar.)

Posted 庚xiao午

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Homework (7th,Mar.)相关的知识,希望对你有一定的参考价值。

第一题:

技术分享
 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 }
View Code
技术分享
 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 }
View Code

技术分享

以上是关于Homework (7th,Mar.)的主要内容,如果未能解决你的问题,请参考以下文章

7th-Activity基础

February 14th, 2018 Week 7th Wednesday

7th week blog

February 12th, 2018 Week 7th Monday

May 7th 2017 Week 19th Sunday

September 7th 2016 Week 37th Wednesday