zoukankan      html  css  js  c++  java
  • 关于java的this关键字和引用(随手记更新中)

     1 public class demo1 {
     2 
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         Dog dog1=new Dog(5,"小白");
     6         Person p1=new Person(dog1,25,"Leng");
     7         p1.ShowInfo();
     8         p1.dog.ShowInfo();
     9 
    10     }
    11 
    12 }
    13 //定义一个人类
    14 class Dog
    15 {
    16     int age;
    17     String name;
    18     public Dog(int age,String name)
    19     {
    20         this.age=age;
    21         this.name=name;
    22     }
    23     public void ShowInfo()
    24     {
    25         System.out.println("狗的名字是"+name);
    26     }
    27 }
    28 class Person
    29 {
    30 int age;
    31 String name;
    32 Dog dog;//引用类型
    33 //写本类的一个构造方法
    34 public Person(Dog dog,int age,String name)
    35 {
    36     this.age=age;
    37     this.name=name;
    38     this.dog=dog;
    39 }
    40 public void ShowInfo()
    41 {
    42     System.out.println("这个人的名字是"+name);
    43 }
    44 }

    java虚拟机会给每个对象分配this ,代表当前对象。

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

  • 相关阅读:
    BiLiBiLi爬虫
    12-UE4-控件类型
    11-UE4-UMG UI设计器
    10-UE4-蓝图定义简介
    UE4-目录结构简介
    UE4-字符串
    UE4-基类
    Redis-事物
    Redis的主从配置
    Redis持久化-AOF
  • 原文地址:https://www.cnblogs.com/zssblog/p/7405184.html
Copyright © 2011-2022 走看看