欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 成长初学者记录篇,OOP 类 Day02

程序员文章站 2022-06-21 14:45:47
...

0.方法签名:方法名+参数列表
1.方法的重载(Overload):
1)发生在同一个类中,方法名相同,参数列表不同,方法体不同
2)编译器在编译时会根据方法的签名自动绑定调用的方法
2.构造方法:
1)给成员变量赋初值
2)与类同名,没有返回值类型
3)在创建对象时被自动调用
4)若自己不写构造方法,则编译器默认一个无参构造方法,
若自己写了构造方法,则不再默认提供
5)可以重载的
3.this:指代当前对象,哪个对象调用方法它指的就是哪个对象
只能用在方法中,方法中访问成员变量之前默认有个this.
this的用法:
1)this.成员变量名--------------访问成员变量
2)this.方法名()----------------调用方法(一般不用)
3)this()-----------------------调用构造方法
4.null和NullPointerException
5.引用类型之间画等号

class Student {
String name; //成员变量  
  int age;
  String address;
    Student(String name){
    this(name,0);
  }
    Student(String name,int age){
    this(name,age,null);
  }
   Student(String name,int age,String address){
    this.name = name;
    this.age = age;
    this.address = address;
  }
}

成员变量和局部变量是可以同名的
------用的时候,采取的是就近原则
------此时访问成员变量是不能省略this.的

Student zs = new Student("zhangsan",25,"LF");
Student ls = new Student("lisi",26,"JMS");
class Student {
String name;//成员变量
int age;
String address;
Student(string name,int age,String address){
this.name = name;
this.age = age;
this.addres;
}

void study(){
System.out.println(name+"学习");
}
void say(){
System.out.printlnt("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
}
}

字符母、数字、_和$字符------仅在特殊情况下才使用

public stitac void main(String[ ] args){
Student zs = new Student(); 
zs.name = "zhangsan";
zs.age = 25;
zs.address = "LF";

zs.study();
zs.sayHi();

Student ls = new Student();
ls.name = "lisi";
ls.age = 26;
ls.address = "JMS";
ls.study();
ls.sayHi();

Student ww = new Student();
ww.study();
ww.sayHi();
}
相关标签: 方法