标签:
public class People { /*定义一个人类,人类中有以下属性:姓名(name),性别(sex),年龄(age),再定义一个测试类 创建一个"张三"的对象 姓名:张三 性别:男 年龄:20 创建一个"李四"的对象 姓名:李四 性别:女 年龄:21 最后输出张三与李四的信息*/ public String name; public String sex; public int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public People(String name, String sex, int age) { super(); this.name = name; this.sex = sex; this.age = age; } public static void main(String[] args) { // TODO 自动生成的方法存根 People p1=new People("张三","男",20); System.out.println("姓名:"+p1.getName()); System.out.println("性别:"+p1.getSex()); System.out.println("年龄:"+p1.getAge()); System.out.println(); People p2=new People("李四","女",21); System.out.println("姓名:"+p2.getName()); System.out.println("性别:"+p2.getSex()); System.out.println("年龄:"+p2.getAge()); } }
姓名:张三
性别:男
年龄:20
姓名:李四
性别:女
年龄:21
public class Piggame { // 写一个传奇游戏中的猪类, // 类中有属性:颜色(color)、重量(weight)、攻击力(attack)、准确度(accuracy)。 // 再写一个测试类, // 生成一个猪的对象,将此猪的颜色值为“白色(white)”, // 重量为5,攻击力为50点,准确度为0.8。 // 要求输出此猪的信息格式为: // 一只白色的猪,重量5,攻击为50点血,准确度为0.8,我好怕怕呀 public String color; public double weight; public double attack; public double accuracy; public Piggame(String color, double weight, double attack, double accuracy) { super(); this.color = color; this.weight = weight; this.attack = attack; this.accuracy = accuracy; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public double getAttack() { return attack; } public void setAttack(double attack) { this.attack = attack; } public double getAccuracy() { return accuracy; } public void setAccuracy(double accuracy) { this.accuracy = accuracy; } public static void main(String[] args) { // TODO 自动生成的方法存根 Piggame p=new Piggame("白色",5,50,0.8); System.out.println("一只"+p.getColor()+"的猪,重量"+p.getWeight()+",攻击为"+p.getAttack()+"点血,准确度为"+p.accuracy+",我好怕怕呀"); } }
一只白色的猪,重量5.0,攻击为50.0点血,准确度为0.8,我好怕怕呀
public class Fruit { /*定义一个水果类(fruit),水果类中的有 【属性】:颜色(color)、价格(price)、重量(weigth), 再定义一个<测试类>, 创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g。 然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g。 最后输出: 苹果的颜色、价格、重量、 香蕉的颜色、价格、重量、*/ public static String color; public double price; public double weigth; public Fruit(String color, double price, double weigth) { super(); this.color = color; this.price = price; this.weigth = weigth; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getWeigth() { return weigth; } public void setWeigth(double weigth) { this.weigth = weigth; } public static void main(String[] args) { // TODO 自动生成的方法存根 Fruit apple=new Fruit("红色",5.5,10); System.out.print("苹果的颜色是:"+apple.getColor()+"。"); System.out.print("价格是"+apple.getPrice()+"元。"); System.out.println("重量是"+apple.getWeigth()+"g。"); Fruit banana=new Fruit("黄色",4.2,5); System.out.print("香蕉的颜色是:"+banana.getColor()+"。"); System.out.print("价格是"+banana.getPrice()+"元。"); System.out.println("重量是"+banana.getWeigth()+"g。"); } }
苹果的颜色是:红色。价格是5.5元。重量是10.0g。
香蕉的颜色是:黄色。价格是4.2元。重量是5.0g。
public class Desk { /*定义一个桌子类,属性有:材料、编号、价格, 再定义一个测试类,在测试类中分别创建两张桌子,分别给他们赋值, 最后输出*/ public String cailiao; public int number; public double price; public Desk(String cailiao, int number, double price) { super(); this.cailiao = cailiao; this.number = number; this.price = price; } public String getCailiao() { return cailiao; } public void setCailiao(String cailiao) { this.cailiao = cailiao; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public static void main(String[] args) { Desk d1=new Desk("复合板",1,100); Desk d2=new Desk("红木",2,1000000); System.out.println("材料是:"+d1.getCailiao()+",编号:"+d1.getNumber()+",价格是:"+d1.getPrice()+"元。"); System.out.println("材料是:"+d2.getCailiao()+",编号:"+d2.getNumber()+",价格是:"+d2.getPrice()+"元。"); } }
材料是:复合板,编号:1,价格是:100.0元。
材料是:红木,编号:2,价格是:1000000.0元。
标签:
原文地址:http://www.cnblogs.com/panyiquan/p/5252401.html