第一部分 回顾Tips:
一、什么是方法重载?
两同一不同:同一个类,相同的方法名,参数不同
二、说明Java方法中参数传递机制的体现?
基本数据类型:数值型
引用数据类型:地址值
第二部分 构造器
一、构造器的作用是什么?
1、创建对象
2、初始化对象
二、关于类的属性的赋值,有几种赋值的方式,谈谈先后顺序
默认初始化-显示初始化-构造器初始化-对象.属性、对象.方法
三、this关键字调用的是哪些结构
this:属性、方法、构造器
this:理解为当前对象,当前正在创建的对象
public class Employee {
String name ;
int age ;
String designation ;
double salary ;
//Employee 类的构造器
public Employee(String name){
this.name = name ;
}
//设置age的值
public void empAage(int empAge){
age = empAge ;
}
/*设置designation的值*/
public void empDesignation(String empDesig){
designation = empDesig ;
}
/*设置薪水的值*/
public void empSalary (double empSalary){
salary = empSalary ;
}
/*打印信息*/
public void printEmployee(){
System.out.println("名字:" + name);
System.out.println("年龄:"+age);
System.out.println("职位" + designation);
System.out.println("薪水"+salary);
}
}
四、super关键字
子类可以通过super.属性或super.方法来调用,遇到 父类和子类相同属性名需要用super关键字显示调用
五、this与super的区别
测试代码:
1、新建一个Person1类
public class Person1 {
String name;
int age;
int id = 1000;
public Person1(){}
public Person1(String name){
this.name = name;
}
public Person1(String name,int age){
this.name = name;
this.age = age;
}
public void eat(){
System.out.println("人:吃东西");
}
public void walk(){
System.out.println("人:走路");
}
}
2、新建一个子类
public class Student1 extends Person1{
String major;
int id = 1001;
public Student1(){}
public Student1(String major){
this.major = major;
}
public void eat(){
System.out.println("学生:吃营养食物");
}
public void study(){
System.out.println("学生:学习知识");
//调用当前类的方法
this.eat();
//调用父类的方法
super.eat();
}
public void show(){
//属性不可覆盖
System.out.println("name = " + this.name+ "、age = " + super.age);
//this.可省略
System.out.println("id = " + this.id);
System.out.println("id = " + super.id);
}
}
3、测试样例
public class SuperTest {
public static void main(String[] args) {
Student1 s = new Student1();
//调用show方法
s.show();
System.out.println();
s.study();
}
}
测试结果: