• 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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

    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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输出顺序是先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);
    	}
       
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

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

    super

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

    调用属性

    public class People {
        public int age;
        private String name;
    
        public People(){
    
        }
    
        public People(int age){
            this.age=age;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    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();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

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

    调用构造器

    public class People {
        public int age;
        private String name;
    
        public People(){
    
        }
    
        public People(int age){
            this.age=age;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    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();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    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");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    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();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    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
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

    继承对构造器的影响

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

    Father类

    public class Father {
        public Father(){
            System.out.println("Father");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Son类

    public class Son extends Father{
        public Son(){
            System.out.println("Son");
        }
    
        public static void main(String[] args) {
            Son son=new Son();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

  • 相关阅读:
    新零售六大模式,你学会了吗?
    Win11如何更改默认下载路径?Win11更改默认下载路径的方法
    协议和分层次
    【动态规划】按位与最大的最长子数组
    Vue组件通信应用实践总结
    C++ 对象和类(1)
    肽核酸(PNA)偶联穿膜肽(CCPs)(KFF)3K形成CCPs-PNA|肽核酸的使用方法
    现在大火的低代码是怎么回事?进来聊聊低代码
    Python编写的人工智能美颜系统
    炫酷动漫游戏网站页面设计html页面前端源码
  • 原文地址:https://blog.csdn.net/qq_45325217/article/details/127631895