Java Class08

Java Class08

this

this用于代表当前对象,通常用于构造器中进行参数传递

this可以访问当前类的属性、构造器和方法

访问属性

public class Test {
    
    //访问属性
    private int age;
    private String name;

    public Test(){
    
    //非默认构造器
    }

    //未使用this关键字
    public Test(int age1){
    
    //将获取的实参传递给形参
        age=age1;
    }

    //使用this关键字
    public Test(String name){
    
    //将获取的实参传递给形参
        this.name=name;
    }
}

在这里插入图片描述
访问构造器

public class B {
    
    
    private int age;//初始化age

    public B(){
    
    //定义默认构造器
        System.out.println("B");
    }


	public B(int age){
    
    
		this();//调用构造器
		System.out.println("B+");
	}
    public static void main(String[] args) {
    
    
        B b=new B(100);
    }
}

输出顺序是先B后B+

访问方法

public class Person3  {
    
    
    String name;//定义属性name
    int age;//定义属性age
    String sex;//定义属性sex

    public Person3(){
    
    //定义无参构造器
        System.out.println("无参构造器被调用");
        sex="Male";
        System.out.println("name="+name+"age="+age+"sex="+sex);
    }

    public Person3(String theName){
    
    //定义含参构造器
        this();//调用类下无参构造器
        System.out.println("含参构造器被调用");
        name=theName;
        System.out.println("name="+name+"age="+age+"sex="+sex);
    }

    public Person3(String theName,int theAge){
    
    //定义含参构造器
        this(theName);//调用类下含参构造器
        System.out.println("含参构造器被调用");
        age=theAge;
        System.out.println("name="+name+"age="+age+"sex="+sex);
    }

	public static void main(String args[]){
    
    
		Person p=new Person("Mike",20);
	}
   
}

在这里this关键字用于调用不同的构造器,调用顺序是方法自下而上的调用

super

super关键字用于对父类的属性、构造器和方法进行查找

调用属性

public class People {
    
    
    public int age;
    private String name;

    public People(){
    
    

    }

    public People(int age){
    
    
        this.age=age;
    }
}

public class Student extends People{
    
    
    public Student(){
    
    

    }

    public Student(int age){
    
    
        super.age=age;
    }

    public void method(int age){
    
    
    }
    public static void main(String[] args) {
    
    
        Student student=new Student();

    }
}

在子类通过super关键字调用的时候,可以直接使用super.属性的方法进行调用,与this关键字类似

调用构造器

public class People {
    
    
    public int age;
    private String name;

    public People(){
    
    

    }

    public People(int age){
    
    
        this.age=age;
    }

public class Student extends People{
    
    
    public Student(){
    
    

    }

    public Student(int age){
    
    
        super(age);
        this.age=age;
    }

    public void method(int age){
    
    
    }
    public static void main(String[] args) {
    
    
        Student student=new Student();

    }
}

super和this关键字一样,在使用的时候必须在方法内的第一行

调用方法

public class People {
    
    
    public int age;
    private String name;

    public People(){
    
    

    }

    public People(int age){
    
    
        this.age=age;
    }

    public void  method(){
    
    
        System.out.println("Method");
    }

}

public class Student extends People{
    
    
    public Student(){
    
    

    }

    public Student(int age){
    
    
        super.age=age;
    }

    public void method(){
    
    
        super.method();
        System.out.println("Method2");
    }
    public static void main(String[] args) {
    
    
        Student student=new Student();
        student.method();
    }
}

this与super的异同点
在这里插入图片描述
初始化块
初始化块实际上就是规定一部分区域,在内部完成一些初始化操作等内容

public class A{
    
    
	private int age;
	{
    
    
		System.out.println("1");
	}
	public A(){
    
    
		System.out.println("2");
	}

	public void demo(){
    
    
		System.out.println("3");
	}
	
	public static void main(String args[]){
    
    
		A a=new A();
		a.demo();
	}
}

输出1、2、3
从上可以得出,初始化块是最先执行的内容,然后再实例化的过程中调用了默认构造器,所以会调用构造器方法,而成员方法只有在调用方法时才会被使用

继承对构造器的影响

如果在继承中使用构造器,会先调用父类的构造器,然后在调用子类的

Father类

public class Father {
    
    
    public Father(){
    
    
        System.out.println("Father");
    }
}

Son类

public class Son extends Father{
    
    
    public Son(){
    
    
        System.out.println("Son");
    }

    public static void main(String[] args) {
    
    
        Son son=new Son();
    }
}

在这里插入图片描述
这里就是先调用Father的无参构造器,再调用Son的无参构造器

猜你喜欢

转载自blog.csdn.net/qq_45325217/article/details/127631895