OPP作业
Posted 安然罒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OPP作业相关的知识,希望对你有一定的参考价值。
1,定义一个水果类(fruit),水果类中的有
【属性】:颜色(color)、价格(price)、重量(weigth),
再定义一个<测试类>,
创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g。
然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g。
最后输出:
苹果的颜色、价格、重量、
香蕉的颜色、价格、重量、
1 package org.hanqi.pn0120; 2 3 public class Fruit { 4 5 private String color; 6 7 private double price; 8 9 private double weigth; 10 11 public String getColor() { 12 return color; 13 } 14 15 public void setColor(String color) { 16 this.color = color; 17 } 18 19 public double getPrice() { 20 return price; 21 } 22 23 public void setPrice(double price) { 24 this.price = price; 25 } 26 27 public double getWeigth() { 28 return weigth; 29 } 30 31 public void setWeigth(double weigth) { 32 this.weigth = weigth; 33 } 34 35 public Fruit(String color, double price, double weigth) { 36 super(); 37 this.color = color; 38 this.price = price; 39 this.weigth = weigth; 40 } 41 42 public static void main(String[] args) 43 { 44 Fruit apple=new Fruit( "红色",5.5,10); 45 46 System.out.println("苹果的颜色是"+apple.getColor()+",价格是"+apple.getPrice()+",重量是"+apple.getWeigth()+"g"); 47 48 Fruit banana=new Fruit("黄色",4.2,5); 49 50 System.out.println("香蕉的颜色是"+banana.getColor()+",价格是"+banana.getPrice()+",重量是"+banana.getWeigth()+"g"); 51 } 52 53 }
2、定义一个人类,人类中有以下属性:姓名(name),性别(sex),年龄(age),再定义一个测试类
创建一个"张三"的对象
姓名:张三
性别:男
年龄:20
创建一个"李四"的对象
姓名:李四
性别:女
年龄:21
最后输出张三与李四的信息
1 package org.hanqi.pn0120; 2 3 public class People { 4 5 private String name; 6 7 private String sex; 8 9 private int age; 10 11 public String getName() { 12 return name; 13 } 14 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 public String getSex() { 20 return sex; 21 } 22 23 public void setSex(String sex) { 24 this.sex = sex; 25 } 26 27 public int getAge() { 28 return age; 29 } 30 31 public void setAge(int age) { 32 this.age = age; 33 } 34 35 public People(String name, String sex, int age) { 36 super(); 37 this.name = name; 38 this.sex = sex; 39 this.age = age; 40 } 41 42 public static void main(String[] args){ 43 44 People people1=new People("张三","男",20); 45 46 System.out.println("姓名:"+people1.getName()+"\n性别:"+people1.getSex()+"\n年龄:"+people1.getAge()); 47 48 System.out.println(); 49 50 People people2=new People("李四","女",21); 51 52 System.out.println("姓名:"+people2.getName()+"\n性别:"+people2.getSex()+"\n年龄:"+people2.getAge()); 53 54 } 55 }
3、定义一个桌子类,属性有:材料、编号、价格,再定义一个测试类,在测试类中分别创建两张桌子,分别给他们赋值,最后输出
1 package org.hanqi.pn0120; 2 3 public class Desk { 4 5 private String cailiao; 6 7 private int binahao; 8 9 private double price; 10 11 public String getCailiao() { 12 return cailiao; 13 } 14 15 public void setCailiao(String cailiao) { 16 this.cailiao = cailiao; 17 } 18 19 public int getBinahao() { 20 return binahao; 21 } 22 23 public void setBinahao(int binahao) { 24 this.binahao = binahao; 25 } 26 27 public double getPrice() { 28 return price; 29 } 30 31 public void setPrice(double price) { 32 this.price = price; 33 } 34 35 public Desk(String cailiao, int binahao, double price) { 36 super(); 37 this.cailiao = cailiao; 38 this.binahao = binahao; 39 this.price = price; 40 } 41 42 43 public static void main(String[] args) 44 { 45 Desk desk1=new Desk("红木",13,146.6); 46 47 System.out.println("桌子的材料是"+desk1.getCailiao()+",编号是"+desk1.getBinahao()+",价格是"+desk1.getPrice()+"¥。"); 48 49 Desk desk2=new Desk("榆木",92,54.2); 50 51 System.out.println("桌子的材料是"+desk2.getCailiao()+",编号是"+desk2.getBinahao()+",价格是"+desk2.getPrice()+"¥。"); 52 53 } 54 }
4,写一个传奇游戏中的猪类,类中有属性:颜色(color)、重量(weight)、攻击力(attack)、准确度(accuracy)。再写一个测试类,生成一个猪的对象,将此猪的颜色值为“白色(white)”,重量为5,攻击力为50点,准确度为0.8。要求输出此猪的信息格式为:一只白色的猪,重量5,攻击为50点血,准确度为0.8,我好怕怕呀!
1 package org.hanqi.pn0120; 2 3 public class Pig { 4 5 private String color; 6 7 private double weight; 8 9 private int attack; 10 11 private double accuracy; 12 13 public String getColor() { 14 return color; 15 } 16 17 public void setColor(String color) { 18 this.color = color; 19 } 20 21 public double getWeight() { 22 return weight; 23 } 24 25 public void setWeight(double weight) { 26 this.weight = weight; 27 } 28 29 public int getAttack() { 30 return attack; 31 } 32 33 public void setAttack(int attack) { 34 this.attack = attack; 35 } 36 37 public double getAccuracy() { 38 return accuracy; 39 } 40 41 public void setAccuracy(double accuracy) { 42 this.accuracy = accuracy; 43 } 44 45 public Pig(String color, double weight, int attack, double accuracy) { 46 super(); 47 this.color = color; 48 this.weight = weight; 49 this.attack = attack; 50 this.accuracy = accuracy; 51 } 52 53 public static void main(String[] args) { 54 55 Pig pig=new Pig("白色",5,50,0.8); 56 57 System.out.println("一只"+pig.getColor()+"的猪,重量"+pig.getWeight()+"," 58 + "为"+pig.getAttack()+"点血,准确度为"+pig.getAccuracy()+",我好怕怕呀!"); 59 60 } 61 62 }
以上是关于OPP作业的主要内容,如果未能解决你的问题,请参考以下文章
HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段