面向对象编程无参,this,指针溢出
什么时候用构造器赋值?
看创建对象是为了干什么?
- 如果说创建对象仅仅是为了调用这个类的方法,建议使用无参构造器
- 如果说创建对象的时候需要使用到对象的某个属性,可以使用构造器赋值
大家养成一个习惯!
-
如果一个类我们手写了构造器
-
原来的无参构造器失效
-
我们养成的一个习惯:保证每个类都有无参构造器
-
需要每一个类里面写了有参构造器,同时也写出无参构造器
public class Ch01 {
public static void main(String[] args) {
// person是Person类的对象,是Person类的实例
Person person = new Person();
// 对象有什么功能?
// 对象可以调属性,可以调方法
// 对象调用的属性和方法
person.name = "小明";
person.age = 20;
// person.eat();
Car car = new Car();
car.brand = "奔驰";
car.color = "黑色";
// car.run();
// person.travel(car);
// p2出生,就会有name,age,wife
Wife wife = new Wife();
wife.name = "李四";
wife.age = 25;
// p2一创建出来,所有的属性都会跟着初始化
Person p2 = new Person("张三",25,wife);
// p3一创建出来,就只有name和age,没有wife
Person p3 = new Person("王五",26);
}
}
实例练习题
需求:
创建一个Person类,有name、age属性
创建一个Debit类,有cardId、password、balance属性
Person类有一个开户的方法:openAccount、ecoIn 余额增加、ecoOut余额减少
Debit类有一个显示银行卡信息的方法:getInfo()
赋值的方式:构造器、直接通过属性方式赋值
开户:给Person类的Debit属性赋值,Debit初始化时需要给cardId,password,balance赋值
最终在Demo类中测试相关功能
Demo.java
package com.jsoft.morning.test01;
/**
* 有一个Person类,有name,age属性
* 有一个Debit类,有cardId,password,balance(余额)属性
*
* Person类有一个开户的方法,openAccount,in(余额增加),out(余额减少,
* 取款时要判断余额)
* Debit类中有一个显示银行卡信息的方法。
*
* 赋值的方式:
* 1.构造器(建议)
* 2.直接通过.属性的方式赋值
*
* 分析:
* 开户:给Person类的Debit属性赋值,Debit初始化时需要给cardId,password,balance赋值
* 最终在Demo类中测试相关功能
*
* 加功能:键盘输入,存款和取款需要比对密码。
* 加键盘输入:
* 哪些地方是需要我们来输入的?
* 1.密码,余额
* 2.存多少钱
* 3.取多少钱
*
* 在哪里写键盘输入Scanner写在哪里?
* Scanner应该出现在main里
*
*/
public class Demo {
public static void main(String[] args) {
Person person = new Person("张三",25);
Debit debit = new Debit("665544998877","123456",100);
person.openAccount(debit);
person.in(151);
person.out(200);
person.out(100);
}
}
Person.java
package com.jsoft.morning.test01;
/**
* 大家养成一个习惯!
* 如果一个类我们手写了构造器
* 原来的无参构造器失效
* 我们养成的一个习惯:保证每个类都有无参构造器
*/
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 openAccount(Debit debit) {
this.debit = debit;
// 开户成功,显示一下开户信息
debit.show();
show();
}
public void in(double money) {
// 存款:修改余额并且重新赋值
debit.balance += money;
System.out.println("存款成功!余额为:" + debit.balance);
}
public void out(double money) {
// 取款:修改余额并且重新赋值
if(money <= debit.balance){
debit.balance -= money;
System.out.println("取款成功!余额为:" + debit.balance);
}else {
System.out.println("余额不足!余额为:" + debit.balance);
}
}
public void show() {
System.out.println("姓名:" + name + ",年龄:" + age);
}
}
Debt.java
public class Debit {
String cardId;
String password;
double balance;
public Debit(){}
public Debit(String cardId, String password, double balance) {
this.cardId = cardId;
this.password = password;
this.balance = balance;
}
public void show() {
System.out.println("卡号:" + cardId + ",余额:" + balance);
}
}
**this 关键字 **
-
this关键字
-
this代表的是当前类的对象,this代表的是当前方法的调用者
-
this既然是代表方法的调用者,它实际上和对象的作用是一样的
-
既可以调属性,也可以调方法。
-
一个类中可以有什么?
-
属性,方法,构造器
-
this不能用在static方法中。
1.开发中,this通常用在什么位置!
2.通常用在赋值,构造器赋值。
3.this关键字可以用来访问本类的属性,方法、构造器
4.this用于区分当前类的属性和局部变量
5.访问成员方法的语句:this。方法名(参数列表)
6.访问构造器语法:this(参数列表);注意只能在构造器中使用(即只能在构造器中访问另外一个构造器,必须放在第一条语句)
7.this不能在类定义的外部使用,只能在类定义的方法中使用
this 的注意事项和使用细节
public class Ch02 {
String str;
int i;
public Ch02(){
}
public Ch02(String str) {
this.str = str;
}
public Ch02(String str,int i){
// this.str = str;
// 调用构造器
// 不需要写任何的名字
/*
this调用构造器的要求
1、必须在构造器中使用this调用构造器
2、必须是第一句话(第一行代码)
*/
this(str);
this.i = i;
}
public void show(String str) {
/*
当使用对象调用属性时,调用的是成员变量
*/
// Ch02 c = new Ch02();
// this(str);
this.str = str;
this.info();
// info();
}
public void info(){
System.out.println(str + "," + i);
}
public static void main(String[] args) {
// Ch02 c = new Ch02();
// c.show("哈哈");
// System.out.println(c.str);
// Ch02 c2 = new Ch02("呵呵");
// c2.info();
Ch02 c3 = new Ch02("嘿嘿",20);
c3.info();
Person person = new Person("张三",25);
}
}
封装:代码层面、思想层面
代码层面:
- 属性私有化,所有的属性都使用private封装
-
提供一个公有的set、get方法
-
set方法可以限制和检验set方法传入的参数是否合法
-
get方法能够按照客户的期望返回格式化的数据
正确定义一个类
-
所有的属性私有化
-
个属性都有对应的set、get方法
Ch01.java
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());
}
}
Ch01.java用这个方法(Dog dog = new Dog(“黑色”,2);)引用
Dog.java
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;
}
}
空指针异常
-
什么情况下会出现空指针异常:
-
引用数据类型的默认值(初始值是null,空引用)
5个异常:
-
1、数组下标越界
-
2、内存溢出(错误)
-
3、空指针
-
4.字符串下标越界
-
5.数字格式化异常
关键字
IDEA 常用快捷键
包的命名
常用包: