java类的学习笔记

***最近在学英文版java,该随笔仅用于学习***

在类中可直接定义variable变量和method方法

  方法method定义方法如void showInfo() {...}//打印所有变量,特别用于变量定义为private私有的情况

  和类名同名的叫默认构造函数default constructor,如果要overload重载构造函数(更改argument参数),一定要写一个没有参数的default constructor。否则不重载就不用写

  当default constructor重载更多参数时,可以在有少的参数default constructor中写入this(参数1,参数2,默认给定值)

  通过this指向该类初始定义的variable,可以看到this.后面的颜色(eclipse中为蓝色)对应初始定义也为蓝色,若是局部定义则显示棕色

  如:

NetworkUser(String id,String password){
        this(id,password,"123456");
    }
NetworkUser(String id,String password,String email){
        this.id=id;
        this.password=password;
        this.email=email;
    }

  该调用方法是在main函数中,这样调用,也叫做create an object/instance of class

NetworkUser user1=new NetworkUser("yeah","123456");
原文地址:https://www.cnblogs.com/juranus1412/p/12483365.html