JAVA入门到精通-第8讲-this类变量

1167156-20181130145223281-1274343938.png
 封装、继承、多态、抽象四大特征
1167156-20181130145223530-1291882257.png
 多态:一个对象的多种状态,是从继承来的;
 软件的可读性和可维护性是密切相关的;
  1167156-20181130145223798-2024087613.png
 多个age 会造成一定理解上的困难;
  所以,带一个this;
  这样比较好理解了,this是属于一个对象的;
1167156-20181130145224194-1154137297.png

 this----"我的"---代词;
 this 是属于一个具体的对象的,而不是属于一个类的;
1167156-20181130145224502-62236735.png
 JAVA虚拟机给每个对象分配一个虚拟机,代表当前对象

1167156-20181130145224784-1577139361.png
 
1167156-20181130145225052-974705560.png
 
1167156-20181130145225333-718700106.png
 
1167156-20181130145225641-1037572117.png
 
dog引用的名存放在P1里面,但是内存放在dog1
1167156-20181130145225854-1791860216.png
 

this是不能在外面去使用的;
只能 在类定义的 时候去使用;
1167156-20181130145226177-2134147626.png
 构造方法(构造函数)就是用来初始化成员变量的;
 
  类变量和类方法:
1167156-20181130145226442-1326709342.png
 
  如果有公共的空间;可以共同的操作;
  static int total = 0;
1167156-20181130145227001-448574954.png
 如果加了static,编译器可以准确的理解到total
 是属于大家的,而不是属于某个小孩子的;

   ch1,ch2,ch3都可访问到 静态变量total;
1167156-20181130145227366-1041334080.png
 
  或者直接用类名也可以访问到静态变量total:
1167156-20181130145227630-793030245.png

java面向对象编程(2)--this

一个问题?

请大家看一段代码:(Demo112.java)

*重点*this是属于一个对象,不属于类的。

java虚拟机会给每个对象分配this,代表当前对象。坦白的讲,要明白this不是件容易的事

注意事项:this不能在类定义的外部使用,只能在类定义的方法中使用

/* this的必要性 */ public class Demo112{ public static void main(String []args){ Dog dog1=new Dog(2,"大黄"); Person p1=new Person(dog1,23,"郭德纲"); Person p2=new Person(dog1,24,"刘谦"); p1.showInfo(); p1.dog.showInfo(); } } //定义一个人类 class Person{ //成员变量 int age; String name; Dog dog;//引用类型 public Person(Dog dog,int age,String name){ //可读性不好 //age=age; //name=name; this.age=age; //this.age指this代词指定是成员变量age this.name=name; //this.name指this代词指定是成员变量name this.dog=dog; } //显示人名字 public void showInfo(){ System.out.println("人名是:"+this.name); } } class Dog{ int age; String name; public Dog(int age,String name){ this.age=age; this.name=name; } //显示狗名 public void showInfo(){ System.out.println("狗名叫"+this.name); } }
this.age=age; this.name=name;
x
1
/*
2
    this的必要性
3
*/
4
public class Demo112{
5
    public static void main(String []args){
6
        Dog dog1=new Dog(2,"大黄");
7
        Person p1=new Person(dog1,23,"郭德纲");
8
        Person p2=new Person(dog1,24,"刘谦");
9
        p1.showInfo(); 
10
        p1.dog.showInfo();
11
    }
12
}
13
//定义一个人类
14
class Person{
15
    //成员变量
16
    int age;
17
    String name;
18
    Dog dog;//引用类型
19
    public Person(Dog dog,int age,String name){
20
        //可读性不好
21
        //age=age;
22
        //name=name;
23
        this.age=age; //this.age指this代词指定是成员变量age
24
        this.name=name; //this.name指this代词指定是成员变量name
25
        this.dog=dog;
26
    }
27
    //显示人名字
28
    public void showInfo(){
29
        System.out.println("人名是:"+this.name);
30
    }
31
}
32
 
33
class Dog{
34
    int age;
35
    String name;
36
    public Dog(int age,String name){
37
        this.age=age;
38
        this.name=name;
39
    }
40
    //显示狗名
41
    public void showInfo(){
42
        System.out.println("狗名叫"+this.name);
43
    }
44
}
1167156-20181130145228055-128586955.png

-------------------------------------------------------------------------------

类变量--提出问题?

提出问题的主要目的就是让大家思考解决之道。


public class Demo113{ public static void main(String []args){ /* int total=0; Child ch1=new Child(3,"妞妞"); ch1.joinGame(); total++; Child ch2=new Child(4,"小小"); ch2.joinGame(); total++; */ Child ch1=new Child(3,"妞妞"); ch1.joinGame(); Child ch2=new Child(4,"小小"); ch2.joinGame(); Child ch3=new Child(5,"大大"); ch3.joinGame(); System.out.println("共有="+Child.total); } } //定义小孩类 class Child{ int age; String name; //static公共函数,total是静态变量,因此它可以被任何一个对象访问 static int total=0; public Child(int age,String name){ this.age=age; this.name=name; } public void joinGame(){ total++; System.out.println("有一个小孩加入了"); } }
 
1
public class Demo113{
2
    public static void main(String []args){
3
    /*  int total=0;
4
        Child ch1=new Child(3,"妞妞");
5
        ch1.joinGame();
6
        total++;
7
        Child ch2=new Child(4,"小小");
8
        ch2.joinGame();
9
        total++;
10
    */
11
        Child ch1=new Child(3,"妞妞");
12
        ch1.joinGame();
13
        Child ch2=new Child(4,"小小");
14
        ch2.joinGame();
15
        Child ch3=new Child(5,"大大");
16
        ch3.joinGame();
17
        System.out.println("共有="+Child.total);
18
    }
19
}
20
//定义小孩类
21
class Child{
22
    int age;
23
    String name;
24
    //static公共函数,total是静态变量,因此它可以被任何一个对象访问
25
    static int total=0;
26
    public Child(int age,String name){
27
        this.age=age;
28
        this.name=name;
29
    }
30
    public void joinGame(){
31
        total++;
32
        System.out.println("有一个小孩加入了");
33
    }
34
}
1167156-20181130145228592-1857031286.png
 ch1/ch2/ch3  的输出结果都是3; 共享static静态变量的内存;












转载于:https://www.cnblogs.com/xuxaut-558/p/10044151.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值