Java面向对象(二)
this关键字
在java中,this关键字是一个最重要的概念。使用this关键字可以完成以下的操作:
- 调用类中的属性
- 调用类中的方法或构造方法
- 表示当前对象:在方法被调用的过程中,哪个对象调用了方法,在方法内的this就表示谁
- 可以使用当前的类名.this
public class Test2{
public static void main(String[] args){
Cat cat = new Cat();
cat.setName("咪咪");
cat.setAge(3);
cat.eat();
}
}
class Cat{
//属性的封装
private String name;//成员变量,在类中定义
private int age;//成员变量,在类中定义
//getter and setter
//对外提供一个对name属性设值的方法
public void setName(String name){//name=咪咪
this.name=name;//this代表的是当前对象
}
//对外提供一个获取name属性的方法
public void getName(String name){
return name;
}
//对外提供一个对age属性设值的方法
public void setAge(int age){
this.age=age;
}
//对外提供一个获取age属性的方法
public void getAge(int age){
return age;
}
public void eat(){
//在方法中使用this调用类中的其它方法或属性,this可以省略
System.out.println("我是"+this.getName()+",我爱吃鱼")
//System.out.println("我是"+Cat.this.getName()+",我爱吃鱼")
//System.out.println("我是"+Cat.this.name()+",我爱吃鱼")
}
}
}
值传递与引用传递
引用传递的本质是传递内存空间地址,而非值本身,通过内存图充分理解栈内存和堆内存
- 值传递
public class ValueDemo{
public static void main(String[]args){
int x = 10;
method(x);
System.out.println("x="+x);
}
public static void method(int mx){
mx=20;
}
}
2. 引用传递
public class RefDemo1{
public static void main(String[] args){
Duck d = new Duck();
method(d);
System.out.println("Duck age="+d.age);
}
public static void method(Duck duck){
duck.age=5;
}
}
class Duck{
int age=2;//省略封装
}
- String 传递
字符串本身就是一个对象
public class RefDemo2{
public static void main(String[] args){
String name="小飞";
method(name);
System.out.println("name="+name);
}
public static void method(String sname){
sname="小备";
}
}
4. String 传递
public class RefDemo3{
public static void main(String[] args){
Person p=new Person();
method(p);
System.out.println("person name="+p.name);
}
public static void method(Person p){
p.name="备备";
}
}
class Person{
String name="飞飞";//省略封装
}
对象的一对一关系
表达对象的一对一关系,其实就是把对象类型作为属性定义
两个对象之间的一对一关系:
比如:
一个英雄(Hero)对一个兵器(Weapon)
代码如何表示?
双向一对一
单向一对一
public class Test5{
public static void main(String[] args){
Hero hero=new Hero("刘备",300);
Weapon weapon = new Weapon("双股剑",3);
//把两个对象关联起来
hero.setWeapon(weapon);
weapon.setHero(hero);
//通过英雄来获取他的信息
String name=hero.getName();
int age=hero.getAge();
Weapon w=hero.getWeapon();
System.out.println("我是"+name+",我"+age+"岁,我的武器是: "+w.getName()+",排行"+w.getGrade()+"级");
}
}
//英雄类
class Hero{
private String name;
private int age;
private Weapon weapon;//一对一
public void setWeapon(Weapon weapon){
this.weapon=weapon;
}
public Weapon getWeapon(){
return weapon;
}
public Hero(){}
public Hero(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public String getName(int age){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}
//兵器类
class Weapon{
private String name;
private int grade;
public Weapon(){}
public Weapon(String name,int grade){
this.name=name;
this.grade=grade;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setGrade(int grade){
this.grade=grade;}}
static关键字
static作用大,内存只有独一份,同类对象都共享,三种用法各不同,谨记静态初始化,它与对象创建没关系
static关键字的作用:
1.使用static关键字修饰一个属性
声明为static的变量实质上就是全局变量
2.使用static关键字修饰一个方法
通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法
3.使用static关键字修饰一个类(内部类)
4.静态变量或方法不属于对象,依赖类
5.静态变量是全局变量,生命周期从类被加载后一直到程序结束
6.静态变量只存一份,在静态方法区中存放
7.静态变量是本来所有对象共享一份
8.建议不要使用对象名去调用静态数据,直接使用类名调用
9.static修是一个方法,那么该方法属于类,不属于对象,直接用类名调用
public class Test6{
public static void main(String[] args){
Role beibei=new Role("刘备","蜀国");
Role yunchang =new Role("云长","蜀国");
Role feifei=new Role("张飞","蜀国");
System.out.println(beibei.geiInfo());
System.out.println(yunchang.geiInfo());
System.out.println(feifei.geiInfo());
}
}
class Role{
private String name;
private String country;
public Role(String name,String country){
this.name=name;
this.country=country;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;}
}
public void setCountry(String country){
this.country=country;
}
public String getCountry(){
return country;
public String getInfo(){
return "name="+name+",country="+country;}
}
public class Test6{
public static void main(String[] args){
Role beibei=new Role("刘备");
Role yunchang =new Role("云长");
Role feifei=new Role("张飞");
System.out.println(beibei.geiInfo());
System.out.println(yunchang.geiInfo());
System.out.println(feifei.geiInfo());
}
}
class Role{
private String name;
private static String country="蜀国";//静态变量(全局变量)
public Role(String name,String country){
this.name=name;
this.country=country;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;}
}
public void setCountry(String country){
this.country=country;
}
public String getCountry(){
return country;
public String getInfo(){
return "name="+name+",country="+country;}
}