知错能改善莫大焉
6.给客户创建账号,并给余额赋值5.类作为数据类型声明变量4.值传递内存解析3.空指针2.局部变量必须初始化1.定义变量语句 数据类型 变量名 = 变量值;
6.给客户创建账号,并给余额赋值
package com
.atguigu
.exer
;
public class CustomerTest1 {
public static void main(String
[] args
) {
Bank1 bank1
= new Bank1();
bank1
.addCustomer("Jane", "Smith");
bank1
.getCustomers(0).setAccount(new Account1(2000));
bank1
.getCustomers(0).getAccount().withraw(500);
double balance
= bank1
.getCustomers(0).getAccount().getBalance();
System
.out
.println("客户: " + bank1
.getCustomers(0).getFirstName()+ " 的账户余额为: " + balance
);
}
}
package com
.atguigu
.exer
;
public class Account1 {
private double balance
;
public Account1(double int_balance
) {
balance
= int_balance
;
}
public double getBalance() {
return balance
;
}
public void deposit(double amt
) {
if (amt
> 0) {
balance
+= amt
;
System
.out
.println("存款成功,余额为:" + balance
);
}
}
public void withraw(double amt
) {
if (balance
< amt
) {
System
.out
.println("余额不足,请确认后重新输入");
return;
}
balance
-= amt
;
System
.out
.println("取款成功,余额为:" + balance
);
}
}
package com
.atguigu
.exer
;
public class Customer1 {
private String firstName
;
private String lastName
;
private Account1 account
;
public Customer1(String f
,String l
){
firstName
= f
;
lastName
= l
;
}
public Account1
getAccount() {
return account
;
}
public void setAccount(Account1 account
) {
this.account
= account
;
}
public String
getFirstName() {
return firstName
;
}
public String
getLastName() {
return lastName
;
}
}
package com
.atguigu
.exer
;
public class Bank1 {
private Customer1
[] customers
;
private int numberOfCustomers
;
public Bank1() {
customers
= new Customer1[10];
}
public void addCustomer(String f
, String l
) {
Customer1 cust
= new Customer1(f
, l
);
customers
[numberOfCustomers
++] = cust
;
}
public int getNumOfCustomers() {
return numberOfCustomers
;
}
public Customer1
getCustomers(int index
) {
if (index
>= 0 && index
< numberOfCustomers
) {
return customers
[index
];
}
return null
;
}
}
5.类作为数据类型声明变量
package com
.atgugui
.exer
;
public class PassObject {
public static void main(String
[] args
) {
PassObject test
= new PassObject();
Circle c
= new Circle();
test
.printAreas(c
, 5);
System
.out
.println("now radius is " + c
.radius
);
}
public void printAreas(Circle c
, int time
) {
System
.out
.println("Radius\t\tArea");
for (int i
= 1; i
<= time
; i
++) {
c
.radius
= i
;
System
.out
.println(c
.radius
+ "\t\t" + c
.findArea());
}
c
.radius
= time
+ 1;
}
}
4.值传递内存解析
package com
.atguigu
.java1
;
public class TransferTest3 {
public static void main(String
[] args
) {
TransferTest3 test
= new TransferTest3();
test
.first();
}
public void first(){
int i
= 5;
Value v
= new Value();
v
.i
= 25;
second(v
,i
);
System
.out
.println(v
.i
);
}
public void second(Value v
,int i
){
i
= 0;
v
.i
=20;
Value val
= new Value();
v
= val
;
System
.out
.println(v
.i
+ " " + i
);
}
}
class Value {
int i
= 15;
}
//错因:调用second方法时,Value val = new Value(); //未意识到新造对象会在堆中开辟新的空间,以及v = val; //这一行赋值错误.
3.空指针
int[][] arr2 = new int[4][]; System.out.println(arr[0]);//null System.out.println(arr2[0][0]);//空指针,运用内存知识进行理解.
2.局部变量必须初始化
if(num1 > num2){ max3 = num1; }else{ int max3 = num2; }
if(max3 > num3){
int max4 = max1;
}else{
max4 = num3;
}
System.out.println("num2 = " + max4);//会报错,必须单独定义新变量才能执行语句.
正确做法:
int max3; int max4; if(num1 > num2){ max3 = num1; }else{ max3 = num2; }
if(max3 > num3){
max4 = max1;
}else{
max4 = num3;
}
System.out.println("num2 = " + max4);
1.定义变量语句 数据类型 变量名 = 变量值;
使用变量语句 变量名 (=) (变量值);不再加数据类型.
class BitTest{
public static void main(String
[] args
){
int num1
= 10;
int num2
= 20;
int temp
= num1
;
num1
= num2
;
num2
= temp
;
System
.out
.println(num1
);
System
.out
.println(num2
);
int num3
= 10;
int num4
= 20;
int temp1
= num3
& num4
;
num3
= temp1
| num4
;
System
.out
.println(num3
);
}
}