1,定义一个水果类(fruit),水果类中的有
【属性】:颜色(color)、价格(price)、重量(weigth),
再定义一个<测试类>,
创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g。
然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g。
最后输出:
苹果的颜色、价格、重量、
香蕉的颜色、价格、重量、
1.fruit类‘’
1 package com.zuoye; 2 3 public class fruit { 4 5 //颜色 6 private String color; 7 //价格 8 private double price; 9 //重量 10 private double weigth; 11 public String getColor() { 12 return color; 13 } 14 public void setColor(String color) { 15 this.color = color; 16 } 17 public double getPrice() { 18 return price; 19 } 20 public void setPrice(double price) { 21 this.price = price; 22 } 23 public double getWeigth() { 24 return weigth; 25 } 26 public void setWeigth(double weigth) { 27 this.weigth = weigth; 28 } 29 public fruit(String color, double price, double weigth) { 30 super(); 31 this.color = color; 32 this.price = price; 33 this.weigth = weigth; 34 } 35 }
2.测试类
1 package com.zuoye; 2 3 public class ceshi { 4 5 public static void main(String[] args) { 6 7 fruit myapple = new fruit("红色",5.5,10); 8 9 fruit mybanana = new fruit("黄色",4.2,5); 10 11 System.out.println("苹果的颜色 :"+ myapple.getColor()+" 价格 :"+myapple.getPrice()+"元"+" 重量 :"+myapple.getWeigth()+"g"); 12 13 System.out.println("香蕉的颜色 :"+mybanana.getColor()+" 价格 :"+mybanana.getPrice()+"元"+" 重量 :"+mybanana.getWeigth()+"g"); 14 } 15 }
最后输出:
2、定义一个人类,人类中有以下属性:姓名(name),性别(sex),年龄(age),再定义一个测试类
创建一个"张三"的对象
姓名:张三
性别:男
年龄:20
创建一个"李四"的对象
姓名:李四
性别:女
年龄:21
最后输出张三与李四的信息
1.人 类
1 package com.zuoye; 2 3 public class man { 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 man(String name, String sex, int age) { 36 super(); 37 this.name = name; 38 this.sex = sex; 39 this.age = age; 40 } 41 }
2.测试类
package com.zuoye; public class test { public static void main(String[] args) { man myzhangsan = new man("张三","男",20); man mylisi = new man("李四","女",21); System.out.println("姓名:"+myzhangsan.getName()+" 性别:"+myzhangsan.getSex()+" 年龄:"+myzhangsan.getAge()); System.out.println("姓名:"+mylisi.getName()+" 性别:"+mylisi.getSex()+" 年龄:"+mylisi.getAge()); } }
最后输出:
3、定义一个桌子类,属性有:材料、编号、价格,再定义一个测试类,在测试类中分别创建两张桌子,分别给他们赋值,最后输出
1 package com.zuoye; 2 3 public class table { 4 5 //材料 6 private String material; 7 //编号 8 private int number; 9 //价格 10 private double price; 11 12 13 public String getMaterial() { 14 return material; 15 } 16 public void setMaterial(String material) { 17 this.material = material; 18 } 19 public int getNumber() { 20 return number; 21 } 22 public void setNumber(int number) { 23 this.number = number; 24 } 25 public double getPrice() { 26 return price; 27 } 28 public void setPrice(double price) { 29 this.price = price; 30 } 31 public table(String material, int number, double price) { 32 super(); 33 this.material = material; 34 this.number = number; 35 this.price = price; 36 } 37 }
测试:
1 package com.zuoye; 2 3 public class desk { 4 5 public static void main(String[] args) { 6 7 8 table mytable1 = new table("红木",16030701,11000); 9 10 11 System.out.println("一号桌所用材料:"+mytable1.getMaterial()); 12 13 System.out.println("一号桌编号:"+mytable1.getNumber()); 14 15 System.out.println("一号桌价格:"+mytable1.getPrice()); 16 17 18 table mytable2 = new table("紫颤木",16030702,51000); 19 20 System.out.println("二号桌所用材料:"+mytable2.getMaterial()); 21 22 System.out.println("二号桌编号:"+mytable2.getNumber()); 23 24 System.out.println("二号桌价格:"+mytable2.getPrice()); 25 } 26 }
最后输出:
4,写一个传奇游戏中的猪类,类中有属性:颜色(color)、重量(weight)、攻击力(attack)、准确度(accuracy)。再写一个测试类,生成一个猪的对象,将此猪的颜色值为“白色(white)”,重量为5,攻击力为50点,准确度为0.8。要求输出此猪的信息格式为:一只白色的猪,重量5,攻击为50点血,准确度为0.8,我好怕怕呀
1.猪类
1 package com.zuoye; 2 3 public class game { 4 //颜色 5 private String color; 6 //重量 7 private int weigth; 8 //攻击力 9 private int attack; 10 //准确度 11 private double accuracy; 12 13 14 public String getColor() { 15 return color; 16 } 17 public void setColor(String color) { 18 this.color = color; 19 } 20 public double getWeigth() { 21 return weigth; 22 } 23 public void setWeigth(int weigth) { 24 this.weigth = weigth; 25 } 26 public int getAttack() { 27 return attack; 28 } 29 public void setAttack(int attack) { 30 this.attack = attack; 31 } 32 public double getAccuracy() { 33 return accuracy; 34 } 35 public void setAccuracy(double accuracy) { 36 this.accuracy = accuracy; 37 } 38 public game(String color, int weigth, int attack, double accuracy) { 39 super(); 40 this.color = color; 41 this.weigth = weigth; 42 this.attack = attack; 43 this.accuracy = accuracy; 44 } 45 }
2.
1 package com.zuoye; 2 3 public class bag { 4 5 public static void main(String[] args) { 6 7 //一只白色的猪, 8 //重量5, 9 //攻击为50点血, 10 //准确度为0.8, 11 //我好怕怕呀 12 game mybag = new game("白色",5,50,0.8); 13 14 15 System.out.println("一只"+mybag.getColor()+"的猪,"+"重量"+mybag.getWeigth()+ 16 17 "攻击为"+mybag.getAttack()+"点血,"+"准确度为"+mybag.getAccuracy()+",我好怕怕啊!"); 18 } 19 20 }
最后结果为: