public class person{
String name;
long age;
String sex;
public void say(){
System.out.println(this.name+"在说话");
}
public void walk(){
System.out.println(this.name+"在走路");
}
//构造方法。应用场景:创建对象的时候,给对象设置属性
public person(String name,long age,String sex){
//this.name = name;
//this.age = age;
this(name,age);//调用下面的同名函数person
this.sex = sex;
}
//重载:函数名相同,参数不同
public person(String name,long age){
this.name = name;
this.age = age;
}
public static void main(String[] args){
person cgb = new person("cgb",30,"男");
cgb.say();
cgb.walk();
person lcf = new person("lcf",20);
lcf.say();
}
}
Java 构造方法+重载
最新推荐文章于 2024-06-13 17:08:11 发布