基础例题
需求:
创建一个Person
类,有name、age
属性
创建一个Debit
类,有cardId、password、balance
属性Person
类有一个开户的方法:openAccount、ecoIn
余额增加、ecoOut
余额减少Debit
类有一个显示银行卡信息的方法:getInfo()
赋值的方式:构造器、直接通过属性方式赋值
开户:给
Person
类的Debit
属性赋值,Debit
初始化时需要给cardId,password,balance
赋值
最终在Demo
类中测试相关功能
创建Person类
内涵name, age
属性Person
类中创建showInfo()
方式
并在其中建立ecoIn
和ecoOut
方法
并使用openAccount
调用Debit
类创建银行账户
public class Person {
String name;
int age;
Debit debit;
public Person() {}
public Person(String name,int age) {
this.name=name;
this.age = age;
}
public void showInfo() {
System.out.print(name+","+age+"岁"+",");
debit.getInfo();
}
public void openAccount(int idCard,String password) {
Debit debit = new Debit(idCard,password,0.0);
this.debit = debit;
}
public void ecoIn(double money) {
debit.balance += money;
}
public void ecoOut(double money) {
if (money>debit.balance) {
System.out.print("[!] 余额不足 ");
return;
}
debit.balance -= money;
}
}
创建Debit类
内涵cardId, password, blance
属性Debit
类中创建getInfo()
方式
public class Debit {
int cardId;
String password;
double balance;
public Debit() {}
public Debit(int cardId,String password, double balance) {
this.balance = balance;
this.password = password;
this.cardId = cardId;
}
public void getInfo() {
System.out.println(cardId + " 账户目前有 " + balance + " 元!");
}
}
创建Demo类
创建一个新person
对象,传参与进去,建立对象
利用person.openAccount
创建银行账户
利用person
内的方法进行对账户的操作
public class Demo {
public static void main(String[] args) {
Person person = new Person("张三",25);
person.openAccount(1001,"mimamima");
person.showInfo();
person.ecoIn(20);
person.showInfo();
person.ecoOut(30);
person.showInfo();
person.ecoOut(10);
person.showInfo();
Person person2 = new Person("李四",20);
person2.openAccount(1002,"yuyiyuyi");
}
}
this关键字
在Java基础中,this关键字是一个重要的概念
this代表的是当前类的对象,this代表的是当前方法的调用者
this既然是代表方法的调用者,它实际上和对象的作用是一样的
它可以调用类中的属性,调用类中的方法或构造方法
将例题改进
加入可以输入的内容,判断密码
制作类似一个前台系统,需要有提示
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Person person = new Person("张三", 25);
person.openAccount(1001, "mimamima");
while (true) {
System.out.print("[!] 请输入您的密码: ");
String pwd = scanner.next();
if (pwd.equals(person.debit.password)) {
System.out.println("[!] 登录成功!欢迎你,"+person.name);
break;
} else {
System.out.println("[❌] 密码错误!请检查是否输入错误?");
continue;
}
}
while (true) {
System.out.println("[?] 请选择你要进行的操作! 1.存款 2.取款 3.查询信息 4.退出程序");
int chooseId = scanner.nextInt();
switch (chooseId) {
case 1:
System.out.print("[?] 请输入你要存款的金额:");
int addMoney = scanner.nextInt();
person.ecoIn(addMoney);
System.out.println("[!] 存款成功!您目前的余额有: "+person.debit.balance);
System.out.println();
continue;
case 2:
System.out.print("[?] 请输入你要取款的金额:");
int takeMoney = scanner.nextInt();
if (person.debit.balance <= takeMoney) {
System.out.println("[❌] 你的余额不足,无法取出!您目前的余额有: "+person.debit.balance);
continue;
}
person.ecoOut(takeMoney);
System.out.println("[!] 取款成功!您目前的余额有: "+person.debit.balance);
continue;
case 3:
System.out.println("[!] "+person.name+",您好!您目前的余额有: "+person.debit.balance);
continue;
default:
System.exit(0);
}
}
}
}
大体上没有任何变化,只是在Demo
类前台添加了输入的内容
判断输入的内容来提示如何操作
并在Debit
类中创建了getPassword()
的方法Demo
通过getPassword()
来获取debit.password
的值
使整个代码更具有结构化、模块化
面向对象的特征
封装:代码层面、思想层面
代码层面:
属性私有化,所有的属性都要使用private
封装
提供一个公有的set、get
方法set
方法可以限制和检验set
方法传入的参数是否合法get
方法能够按照客户的期望返回格式化的数据
正确定义一个类
所有的属性私有化
每个属性都有对应的set、get
方法
public class Ch01 {
public static void main(String[] args) {
Dog dog = new Dog("黑色",2);
System.out.println(dog.getColor());
dog.setColor("白色");
System.out.println(dog.getColor());
}
}
public class Dog {
private String color;
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return this.color;
}
public Dog(){}
public Dog(String color,int age) {
this.color = color;
this.age = age;
}
}
这样写的好处是什么?
通过此方法,无法再使用dog.color
或dog.age
来获取/更改值
在Dog
的类中,可以定义set
与get
方法,在方法中可以做更多的操作
可以判断值的变量是否符合规定,然后再决定要做什么事情
关键字
abstract | boolean | break | byte | case |
catch | char | class | const | continue |
default | do | double | else | extends |
final | finally | float | for | goto |
if | implements | import | instanceof | int |
interface | long | native | new | package |
private | protected | public | return | short |
static | strictfp | super | switch | synchronized |
this | throw | throws | transient | try |
void | volatile | while | assert | enum |
作用域
作用域的分类
全局变量:也就是属性
局部变量:除属性之外的其他变量
全局变量 | 局部变量 |
---|---|
作用域为整个类 | 作用域为定义它的代码块中 |
可以不赋值直接使用 | 必须赋值后才能使用 |
变量和属性不可以重名 | 变量和属性可以重名 |
可以不加修饰符 | 变量不可以加修饰符 |
可以被本类或其他类使用 | 只能在本类中使用 |
构造器
构造方法又叫构造器,主要作用是完成对新对象的初始化
一些特定:
方法名和类名相同
没有返回值
在创建对象时,系统会自动调用该类的构造器完成对象的初始化
需要注意的是
一个类可以定义多个不同的构造器,即构造器重载
构造器是完成对象的初始化,不是创建对象
在创建对象时,系统自动调用该类的构造方法
一旦定义了构造器,默认的构造器就会被覆盖
8 条评论
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
建议补充发展中国家案例,避免视角局限。
航宝快更新
我宣布 本博客禁止ocean发表评论
你在狗叫什么? 航宝也是你能叫你能亲的
吃了fgb
昨天作业呢