Java——08——关键字package import super

一:package

在这里插入图片描述

在这里插入图片描述

二:import关键字

在这里插入图片描述

自定义类要导包 不然会报错

package com.xx.study;

import java.util.HashMap;

public class Person {
    
    
	
	public static void main(String[] args) {
    
    
		HashMap map =new HashMap();
	}
	
}

三:super

1.super调用属性和方法

在这里插入图片描述当属性重名是,super调用的是父类的,this先找子类的
在这里插入图片描述
Person.java

package com.xx.study;

public class Person {
    
    
	String name;
	int age;
	int id=1001;//身份证号
	public Person() {
    
    }
	public Person(String name,int age) {
    
    
		this.name=name;
		this.age=age;
	}
	public void eat() {
    
    
		System.out.println("吃饭");
	}
	public void sleep(int time) {
    
    
		System.out.println("睡觉"+time+"小时");
	}
}
	

Student.java

package com.xx.study;

public class Student extends Person {
    
    
	String major;
	//和父类都有id,内存中两个id,属性和方法不一样,方法会覆盖,属性不会
    int id=1002;//学号
	public Student() {
    
    
	}

	public Student(String major) {
    
    
		this.major = major;
	}

	public void study() {
    
    
		System.out.println("学习java");
	}

	// 对父类的eat方法重写
	public void eat() {
    
    
		System.out.println("吃减脂餐");
	}
	public void show() {
    
    
		//System.out.println(name+"年龄是"+age);
		//可以用this调用也可以用super,
		System.out.println(this.name+"年龄是"+super.age);
		System.out.println(id);
		System.out.println(this.id);//调用的是子类的id
		System.out.println(super.id);//调用的是父类的id
	}
}

SuperTest.java

package com.xx.study;

public class SuperTest {
    
    
   public static void main(String[] args) {
    
    
	Student s=new Student();
	s.show();
}
}

2.super调用构造器

在这里插入图片描述4.4 在构造器的首行,没有显示的声明“this(形参列表)”或super(形参列表)”,则默认调用的是父类中空参的构造器,默认有一个super()

Person.java

package com.xx.study;

public class Person {
    
    
	String name;
	int age;
	int id=1001;//身份证号
	public Person() {
    
    
		System.out.println("测试在子类构造器中不声明this,或super,默认调用super()");
	}
	public Person(String name,int age) {
    
    
		this.name=name;
		this.age=age;
	}
	public void eat() {
    
    
		System.out.println("吃饭");
	}
	public void sleep(int time) {
    
    
		System.out.println("睡觉"+time+"小时");
	}
}
	

Student.java

package com.xx.study;

public class Student extends Person {
    
    
	String major;
	//和父类都有id,内存中两个id,属性和方法不一样,方法会覆盖,属性不会
    int id=1002;//学号
	public Student() {
    
    
	}

	public Student(String major) {
    
    
		//super();默认有
		this.major = major;
	}
	public Student(String name,int age,String major) {
    
    
		//this.name=name;
		//this.age=age;
	    super(name,age);//调用父类的构造器  ctrl+鼠标左键
	    this.major=major;
	}

	public void study() {
    
    
		System.out.println("学习java");
	}

	// 对父类的eat方法重写
	public void eat() {
    
    
		System.out.println("吃减脂餐");
	}
	public void show() {
    
    
		//System.out.println(name+"年龄是"+age);
		//可以用this调用也可以用super,
		System.out.println(this.name+"年龄是"+super.age);
		System.out.println(id);
		System.out.println(this.id);//调用的是子类的id
		System.out.println(super.id);//调用的是父类的id
		this.eat();
		super.eat();
	}
}

SuperTest.java

package com.xx.study;

public class SuperTest {
    
    
   public static void main(String[] args) {
    
    
	Student s=new Student();
	s.show();
	Student s1=new Student("Jack马",55,"阿里");
	s1.show();
	System.out.println("********************");
	Student s2=new Student();
	s2.show();
}
}

猜你喜欢

转载自blog.csdn.net/x1037490413/article/details/109228369
今日推荐