package class2106date20210521project_1;
//创建类:1.声明一个类//并不需要main入口
public class JokerA {
//2.明确这个新的类的实例有哪些属性;
String name; //规定属性名和属性类型;
int age ;
//3.明确新的类的实例有哪些行为
public void perform() {
System.out.println("所属新类的第一个行为(方法)"+"perform"+" from "+name);
}
public void sport() {
System.out.println("所属新类的第二个行为(方法)"+"sport-basketball"+" from "+name);
}
//===============================================================================================
// //现在我们定义了一个新的类JokerA,它暂时具有两种属性和方法,接下来将类实例化;
// //在实例化的时候需要调用方法和属性,因而写main入口;
// public static void main(String[] args) {
// //创建第一个对象:1.new
// JokerA joker1 = new JokerA(); //new 一个新的对象joker1 以无参构造方法的形式;
// //创建第一个对象:2.访问属性和方法
// joker1.name = "caiXuKun";
// joker1.age = 24 ;
// joker1.perform();
// joker1.sport();
//
// //类是由对象抽象的概念,一个类中可以创建多个实例,具有相似风格的属性和方法;
//
// JokerA joker2 = new JokerA();
// joker2.name = "xiaoZhan";
// joker2.age = 23 ;
// joker2.perform();
// joker2.sport();
//
// }
//=============================================================================================
char sex ; //这里新增加两个属性名,为了实验构造方法
double height;
public void print1() {
System.out.println("名字" + name + "年龄" + age );
}
public void print2() {
this.print1();
System.out.println("名字" + name + "年龄" + age + "性别" + sex );
}
public void print3() {
this.print2();
System.out.println("名字" + name + "年龄" + age + "性别" + sex + "身高" + height);
}
// /*
// * 2.接下来讨论构造方法//有参数的构造方法会将jvm提供的无参构造方法覆盖掉,这里将第一部分全部注释掉
// *
// * 我们在第1部分中讨论了无参构造方法,当属性和方法比较多的时候,该方法显得不那么方便,
// * 这里引入一种有参数的构造方法,参数可以根据属性、方法有任意个,实际开发中,通常使用无参和全参两种
// * 构造方法是指 创建对象没有返回值的特殊方法; 访问修饰符 +类名(参数列表){}
// */
// //1.构造
// public JokerA(String name ,int age ,char sex ,double height) {
// this.name =name ;
// this.age =age;
// this.sex=sex;
// this.height =height; // 所需要的参数需要在前边提前声明,在第18.19行新增
// }
//
// //2.实例化/传参;
// public static void main(String[] args) {
// JokerA joker1 = new JokerA("caiXuKun",24,‘女‘,175.2);
// JokerA joker2 = new JokerA("xiaoZhan",23,‘男‘,180.1);
//
// joker1.perform();
// joker2.sport();
// }
//==============================================================================================
// this 关键字 我们在声明类的实例具有的属性名时已经使用过相应的名字,在定义参数列表时仍然使用这些名字是不合适
//的,首先是程序运行的不支持,其次是违背代码命名规范;这里使用了this 关键字来区分参数名和其他局部变量名字
// this 关键字既可以用来访问属性,this.属性名; 也可以用来访问方法,this.方法名;
// this 还可以用来访问构造方法 this(形参列表),但必须遵循:
// 构造语句this(实参)必须写在本类构造的第一句(因而只能使用一次);
//==============================================================================================
public JokerA(String name,int age) { //仍然支持方法重载
this.age =age; //通过this 访问height 属性;
this.name =name;
}
//==============================================================================================
public JokerA(String name,int age,char sex) {
this.age =age;
this.name =name;
this.sex =sex;
}
public JokerA(String name,int age,char sex,double height) {
this(name,age);
// this(name,age,sex);//Constructor call must be the first statement in a constructor,不支持两个this访问构造方法;
this.sex=sex;
this.height=height;
}
//==============================================================================================
public static void main(String[] args) {
JokerA joker3 = new JokerA("xiaoZhan",23,‘男‘);
joker3.height = 177;
joker3.print1();
System.out.println("====================");
joker3.print2();
System.out.println("====================");
joker3.print3(); //this 访问方法区分前后名字
}
}
jdk1.8 面向对象 第一节 概念、构造方法、this关键字
原文:https://www.cnblogs.com/msslearning/p/14797267.html