OOP作业

Posted 烟_雨_江_南

tags:

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

技术分享
 1 package org.hanqi.pn0120;
 2 
 3 public class Fruit {
 4     /*定义一个水果类(fruit),水果类中的有
 5     【属性】:颜色(color)、价格(price)、重量(weigth),
 6     再定义一个<测试类>,
 7     创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g。
 8     然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g。
 9     最后输出:
10     苹果的颜色、价格、重量、
11     香蕉的颜色、价格、重量、*/
12     private String color;
13     private double price;
14     private double weight;
15     public String getColor() {
16         return color;
17     }
18     public void setColor(String color) {
19         this.color = color;
20     }
21     public double getPrice() {
22         return price;
23     }
24     public void setPrice(double price) {
25         this.price = price;
26     }
27     public double getWeight() {
28         return weight;
29     }
30     public void setWeight(double weight) {
31         this.weight = weight;
32     }
33     public Fruit(String color, double price, double weight) {
34         super();
35         this.color = color;
36         this.price = price;
37         this.weight = weight;
38     }
39     public static void main(String[] args)
40     {
41         Fruit myApple=new Fruit("红色",5.5,10);
42         
43         System.out.println("苹果的颜色是"+myApple.color+" 价格是"+myApple.price+" 重量是"+myApple.weight+"g");
44         
45         Fruit myBanana=new Fruit("黄色",4.2,5);
46         
47         System.out.println("香蕉的颜色是"+myBanana.color+" 价格是"+myBanana.price+" 重量是"+myBanana.weight+"g");
48     }
49 
50 }
Fruit

技术分享

技术分享
 1 package org.hanqi.pn0120;
 2 
 3 public class RenLei {
 4     
 5 
 6 /*定义一个人类,人类中有以下属性:姓名(name),性别(sex),年龄(age),再定义一个测试类
 7 创建一个"张三"的对象
 8 姓名:张三
 9 性别:男
10 年龄:20
11 创建一个"李四"的对象
12 姓名:李四
13 性别:女
14 年龄:21
15 最后输出张三与李四的信息*/
16     
17     private String name;
18     private String sex;
19     private int age;
20     public String getName() {
21         return name;
22     }
23     public void setName(String name) {
24         this.name = name;
25     }
26     public String getSex() {
27         return sex;
28     }
29     public void setSex(String sex) {
30         this.sex = sex;
31     }
32     public int getAge() {
33         return age;
34     }
35     public void setAge(int age) {
36         this.age = age;
37     }
38     public RenLei(String name, String sex, int age) {
39         super();
40         this.name = name;
41         this.sex = sex;
42         this.age = age;
43     }
44     public static void main(String[] args)
45     {
46         RenLei zhangsan=new RenLei("张三","男",20);
47         
48         System.out.println("姓名:"+zhangsan.getName()+" 性别:"+zhangsan.getSex()+" 年龄:"+zhangsan.getAge());
49         
50         RenLei lisi=new RenLei("李四","女",21);
51         
52         System.out.println("姓名:"+lisi.getName()+" 性别:"+lisi.getSex()+" 年龄"+lisi.getAge());
53         
54         
55     }
56 
57 }
RenLei

技术分享

技术分享
 1 package org.hanqi.pn0120;
 2 
 3 public class Desk {
 4     //定义一个桌子类,属性有:材料、编号、价格,再定义一个测试类,
 5     //在测试类中分别创建两张桌子,分别给他们赋值,最后输出
 6     
 7     private String cailiao;
 8     private int numble;
 9     private double price;
10     public String getCailiao() {
11         return cailiao;
12     }
13     public void setCailiao(String cailiao) {
14         this.cailiao = cailiao;
15     }
16     public int getNumble() {
17         return numble;
18     }
19     public void setNumble(int numble) {
20         this.numble = numble;
21     }
22     public double getPrice() {
23         return price;
24     }
25     public void setPrice(double price) {
26         this.price = price;
27     }
28     public Desk(String cailiao, int numble, double price) {
29         super();
30         this.cailiao = cailiao;
31         this.numble = numble;
32         this.price = price;
33     }
34     public static void main(String[] args)
35     {
36         Desk hongmu=new Desk("红木",21,10000);
37         
38         System.out.println("材料:"+hongmu.getCailiao()+" 编号:"+hongmu.getNumble()+" 价格:"+hongmu.getPrice()+"元");
39         
40         Desk zitanmu=new Desk("紫檀木",30,8000);
41         
42         System.out.println("材料:"+zitanmu.getCailiao()+" 编号:"+zitanmu.getNumble()+" 价格:"+zitanmu.getPrice()+"元");
43     }
44 
45 }
Desk

技术分享

技术分享
 1 package org.hanqi.pn0120;
 2 
 3 public class GamePig {
 4     
 5     /*写一个传奇游戏中的猪类,
 6     类中有属性:颜色(color)、重量(weight)、攻击力(attack)、准确度(accuracy)。
 7     再写一个测试类,生成一个猪的对象,
 8     将此猪的颜色值为“白色(white)”,重量为5,攻击力为50点,准确度为0.8。
 9     要求输出此猪的信息格式为:
10     一只白色的猪,重量5,攻击为50点血,准确度为0.8,我好怕怕呀*/
11     
12     private String color;
13     private double weight;
14     private double attack;
15     private double accuracy;
16     public String getColor() {
17         return color;
18     }
19     public void setColor(String color) {
20         this.color = color;
21     }
22     public double getWeight() {
23         return weight;
24     }
25     public void setWeight(double weight) {
26         this.weight = weight;
27     }
28     public double getAttack() {
29         return attack;
30     }
31     public void setAttack(double attack) {
32         this.attack = attack;
33     }
34     public double getAccuracy() {
35         return accuracy;
36     }
37     public void setAccuracy(double accuracy) {
38         this.accuracy = accuracy;
39     }
40     public GamePig(String color, double weight, double attack, double accuracy) {
41         super();
42         this.color = color;
43         this.weight = weight;
44         this.attack = attack;
45         this.accuracy = accuracy;
46     }
47     public static void main(String[] args)
48     {
49         GamePig pig=new GamePig("白色",5,50,0.8);
50         
51         System.out.println("一只"+pig.getColor()+"的猪,重量"+pig.getWeight()+",攻击为"+pig.getAttack()+"点血,准确度为"+pig.getAccuracy()+",我好怕怕呀!");
52         
53     }
54     
55     
56 
57 }
GamePig

技术分享

以上是关于OOP作业的主要内容,如果未能解决你的问题,请参考以下文章

OOP作业

java golang oop 2文章片段

OOP第一次博客作业

OOP作业

OOP作业总结

VSCode自定义代码片段——JS中的面向对象编程