1.
封装一个类Student
(1) 属性:学号、名字、年龄、性别
(2) 方法:吃饭、跑步、睡觉
(3) 属性私有化
(4) 添加访问属性的 get 和 set 方法。
(5) 在类中添加构造方法
无参构造方法
有参构造方法-在构造方法中初始化学生的各个属性值
(6) 重写toString方法,获取学生的各个属性构成的字符串
public class Student {
// 属性:学号、名字、年龄、性别。 属性私有化。
private String num;
private String name;
private int age;
private char sex;
// 在类中添加构造方法
// 无参构造方法
// 有参构造方法-在构造方法中初始化学生的各个属性值
public Student() {
}
public Student(String num, String name, int age, char sex) {
this.num = num;
this.name = name;
this.age = age;
this.sex = sex;
}
// 添加访问属性的 get 和 set 方法。
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
// 方法:吃饭、跑步、睡觉
public void eat() {
System.out.println(getName() + "饿死了,正在狂吃");
}
public void sleep() {
System.out.println(getName() + "困了,倒头就睡");
}
public void run() {
System.out.println(getName() + "吃饱睡好了,正在狂飙");
}
@Override
public String toString() {
return "这是" + getName() + "。他今年" + getAge() + "岁了。是个" + getSex() + "孩。" + "学号是:" + getNum() + "。";
}
}
public class Test {
public static void main(String[] args) {
// 实例化并初始化对象
Student a = new Student("226", "小雪", 22, '女');
// 测试方法
a.eat();
a.run();
a.sleep();
// 输出信息
System.out.println(a.toString());
}
}
2.
定义用户类Customer,
有属性:有姓名、身份证号、联系电话、家庭地址
重写toString,返回String类型,返回他的个人信息。
定义银行账户类Account,
有属性:卡号cid,余额balance,所属用户Customer
银行账户类Account有方法:
(1)重写toString方法,返回String类型,返回卡的详细信息
(2)取钱方法withdraw(),参数自行设计,如果取钱成功返回true,失败返回false
(3)存钱方法save(),参数自行设计,如果存钱成功返回true,失败返回false
在测试类Bank中创建银行账户类对象和用户类对象,并设置信息,与显示信息
public class Account {
// 属性
private String cid;
private double balance;
private Customer customer;
// 构造方法
public Account() {
}
public Account(String cid, double balance, Customer customer) {
this.cid = cid;
this.balance = balance;
this.customer = customer;
}
// 封装属性
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
// 方法
@Override
public String toString() {
return "本银行卡cid为:"+getCid()+",余额为:"+getBalance()
+",所属用户为:"+getCustomer().getName()+"。";
}
public boolean withdraw() {
if(this.getBalance()>0) {
return true;
}return false;
}
public boolean save() {
if(this.getBalance()<500000) {
return true;
}return false;
}
}
public class Customer {
// 属性
private String name;
private String num;
private String phone;
private String address;
// 构造方法
public Customer () {
}
public Customer(String name, String num, String phone, String address) {
this.name = name;
this.num = num;
this.phone = phone;
this.address = address;
}
// 封装方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
// 方法
@Override
public String toString() {
return "用户姓名为:"+getName()+"。身份证号为:"+getNum()+"。"
+ "联系电话为:"+getPhone()+"。家庭住址为:"+getAddress()+"。";
}
}
public class Bank {
public static void main(String [] args) {
//实例化对象
Customer a = new Customer("图图", "13111111222587", "13110086", "翻斗小区翻斗花园1号");
Account b=new Account("11125",327,a);
//输出方法
System.out.println(a.toString());
System.out.println(b.toString());
System.out.println(b.withdraw());
System.out.println(b.save());
}
}