/*创建一个银行账户的类
思路:账户分为一个类;顾客信息分为一个类,其中顾客类中包含账户类信息;
//知识点:类对象的创建;实例变量私有化并进行封装;建立取款与存款功能方法。
*/
public class TotalAccount{
public static void main(String[] args){
Account account1= new Account("1000",2000.0);//创建一个名为account1的Account类对象
Customer c1=new Customer("Jane Smith",account1);//创建一个名为c1的类对象
System.out.println(account1.getBalance());
System.out.println(account1.getId());
System.out.println(c1.getName());
account1.deposit(100.9);
account1.withdraw(800);
account1.withdraw(3000);
}
}
class Customer{
private String name;
Account account;
//建立新对象读取的方法
public Customer(){}
public Customer(String name,Account account){
this.name=name;
this.account=account;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Account getAccount(){
return account;
}
public void setAccount(Account account){
this.account = account;
}
}
class Account{
//实例变量私有化
private String id;
private double balance;
//建立新对象读取的方法
public Account(){}
public Account(String id,double balance){
this.id = id;
this.balance=balance;
}
//建立get和set实例变量方法
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public double getBalance(){
return balance;
}
public void setBalance(double balance){
this.balance = balance;
}
//建立取款方法
public double withdraw(double money){
if(money>balance){
System.out.println("余额不足,取钱失败");
return 0.0;
}
System.out.println("成功取出:"+money);
System.out.println("剩余余额:"+(balance-money));
return balance-money;
}
//建立存款方法
public double deposit(double money){
System.out.println("成功存入:"+money);
System.out.println("剩余余额:"+(balance+money));
return balance+money;
}
}在这里插入代码片
效果图如下:
/* 封装 已知一个类student的代码如下:
class Student{
String name;
int age;
String address;
String zipCode;
String mobile;
}
要求:1、把student的属性私有化,并提供get/set方法以及沟通的构建方法。
2、为student类添加一个getPostAddress方法,要求返回对象的地址和邮编。
*/
public class Student1{
public static void main(String[] args){
Student s1 = new Student();
System.out.println(s1.getPostAddress());
s1.setAddress("北京");
s1.setZipCode("200002");
System.out.println(s1.getPostAddress());
}
}
class Student{
private String name;
private int age;
private String address;
private String zipCode;
private String mobile;
public void student(){};
public void student(String name,int age,String address,String zipCode,String mobile){
this.name =name;
this.age=age;
this.address=address;
this.zipCode=zipCode;
this.mobile=mobile;
}
public String getPostAddress(){
return "地址:"+this.getAddress()+",邮编:"+this.getZipCode();
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return address;
}
public void setMobile(String mobile){
this.mobile=mobile;
}
public String getMobile(){
return mobile;
}
public void setZipCode(String zipCode){
this.zipCode=zipCode;
}
public String getZipCode(){
return zipCode;
}
}在这里插入代码片
效果图如下: