定义对象的属性
public class Dog {
//定义类的属性
private String name;
private String type;
private double price;
private int age;
//无参构造
public Dog() {
}
//有参构造
public Dog( String type, double price, int age) {
this.type = type;
this.price = price;
this.age = age;
}
//生成set get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
定义对象中的方法
package com.ff; import java.util.Scanner; public class person { //在方法外定义的为全局变量 类中的方法都能访问 private String name; private double money; private static double bankMoney;//银行的钱 贷款 静态的 //静态代码块 初始化银行的贷款额度 static { bankMoney = 10000000; } /* public void main(){ System.out.println("1.购买"); System.out.println("2.出售"); System.out.println("3.借钱"); } */ public void shoping(Dog dog){ System.out.println(name+"的金钱为"+this.money); //方法内定义的变量只能在方法里边使用 局部变量 this.money -= dog.getPrice(); //如果初始化对象的钱小于宠物的价钱则提示穷鬼买不起 if (this.money<dog.getPrice()){ System.out.println(this.name+"穷鬼,你暂时买不到任何小狗哦"); }else { System.out.println("给你的小宠物起个名字吧!"); dog.setName(new Scanner(System.in).next()); System.out.println(name+"购买了["+dog.getName()+"]品种是["+dog.getType()+"]价钱为["+dog.getPrice()+"]剩余["+money+"]"); } } public void sell(Dog dog){ System.out.println("飞飞"+this.money); System.out.println("请选择你要出售的宠物的名字"); this.money+=dog.getPrice(); System.out.println(name+"出售了"+dog.getName()+"品种是"+dog.getType()+"价钱为"+dog.getPrice()+"剩余"+money); } public void bankMoney(Dog dog){ System.out.println("为"+this.name+"的狗子取一个名字吧!"); dog.setName(new Scanner(System.in).next()); System.out.println("银行额度"+bankMoney); bankMoney -= dog.getPrice(); System.out.println(name+"购买了"+dog.getName()+"品种是"+dog.getType()+"价钱为"+dog.getPrice()+"剩余额度为"+bankMoney); } public person(String name, double money) { this.name = name; this.money = money; } public person() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public static double getBankMoney() { return bankMoney; } public static void setBankMoney(double bankMoney) { person.bankMoney = bankMoney; } }
调用对象中的方法
package com.ff;
import java.util.Scanner;
public class DogShop {
//全局变量 类中的方法都能用
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Dog d1 = new Dog( "萨摩亚", 120, 1);
Dog d2 = new Dog( "阿拉斯加", 100, 1);
Dog d3 = new Dog("哈士奇", 80, 1);
person ff = new person("飞飞", 800);
person sb = new person("砂壁", 10);
//自己的钱只能自己花
ff.shoping(d1);
sb.shoping(d2);
//ff.sell(d1);
//银行的钱是大家共享的
//ff.bankMoney(d1);
//sb.bankMoney(d3);
}
}
这个博客展示了如何在Java中定义类的属性和方法,并通过实例展示了购物过程,包括购买、出售和银行贷款操作。代码包括Dog类和Person类,以及对应的setter和getter方法,用于操作对象的属性。在DogShop类中,创建了Dog对象并调用了Person对象的方法进行模拟购物操作。
3540

被折叠的 条评论
为什么被折叠?



