• 2020.7.15


    一、今日学习内容

      1、类的属性

         (1)属性:成员属性(全局变量),局部变量

                 成员属性:定义在类中,而在方法外,他的范围归整个类所共享。

    1 public  class Person{
    2    private String name;
    3    private int age;
    4    public static void main(String[] args){
    5         Person p=new Person();
    6         System.out.println(p.name+","+p.age);
    7    }
    8 }

            输出结果:null,0

            通常用户不会直接去访问或修改属性,因为这样的访问和修改是及其危险的,所以通常会对属性进行封装,使用getXXX得到属性值,使用setXXX设置属性值。 

            例:

                     

              局部变量:定义在方法内部,作用范围到方法尾结束。

    1 public class JuBuDemo{
    2    public static void main(String[] args){
    3    }
    4     public void t(){
    5        int x=7;//局部变量
    6        System.out.println("x="+x);
    7     }
    8 }

          (2)this关键字:this指的是当前对象

     1 public class ThisDemo{
     2    int x=5;
     3    public void ThisDemo(int x){
     4        this.x=x;//this.x指的是属性x;方法传入的x,指的是形参x
     5     }
     6     public static void main(String[] args){
     7        ThisDemo demo=new ThisDemo(9);
     8        Systemout.println(demo.x);
     9    }
    10 }

           输出结果:9

          实例: 调用方法

     1 public class ThisDemo{
     2     public static void main(String[] args){
     3     }
     4     public void t(){
     5         System.out.println("t");
     6     }
     7      public void t2(){
     8         this.t();
     9       }
    10 }

          实例:调用无参构造方法

     1 public  class ThisDemo{
     2    int x;
     3    public ThisDemo(){
     4        System.out.println("无参构造函数“);
     5    }
     6     public ThisDemo(int x){
     7         this();
     8         this.x=x;
     9     }
    10 }

           this()指的是调用本类中的无参构造器。

           实例:调用有参构造方法

     1  public  class ThisDemo{
     2    int x;
     3    public ThisDemo(){
     4       System.out.println("无参构造函数“);
     5    }
     6     public ThisDemo(int x){
     7         this();
     8         this.x=x;
     9      }
    10       public ThisDemo(int x,int y){
    11          this(x);
    12          y=20;
    13       }
    14 }

         this(x)指的是调用本类中的有参构造器。

         注:使用this指针时,this关键字必须放在构造器的第一行,否则会报错。

    二、遇到的问题

         今天没有遇到什么太大的问题

    三、明日计划

         继续学习第四章的内容

                    

  • 相关阅读:
    ReflectionException: There is no getter for property named
    iframe发送post请求
    wget已安装但命令没找到
    linux性能观察命令
    ELK搭建
    python之中特性(attribute)与属性(property)有什么区别?
    Django中的日志详解
    创建fastdfs_nginx容器及nginx配置
    2. 顺序表 数据结构与算法(python)
    Ubuntu安装和卸载搜狗输入法
  • 原文地址:https://www.cnblogs.com/wmdww/p/13308487.html
Copyright © 2020-2023  润新知