Live2D
复制代码

12-super和final关键字

super和final关键字

1、super关键字

1.1 认识super关键字

/*
 *   1.super不是引用类型,super中存储的不是内存地址,super指向的不是父类对象
 *   2.super代表的是当前子类对象的父类型特征
 *   
 *   3.什么时候使用super
 *         子类和父类中都有某个数据,如都有name这个属性
 *         如果要在子类中访问父类中的name属性,需要使用  super.
 *         
 *   4.super可以用在什么地方
 *         一、super可以用在成员方法中,不能用在静态方法中
 *         二、super可以用在构造方法中
 */
//经理
package msuper关键字;

public class Manager extends Employ {
	
	String name="一块钱四个";
	
	//成员方法,子类将父类的方法重写了
	public void work(){
		System.out.println("经理在工作");
	}
	
	//成员方法
	public void m1(){
		
		work();//等于this.work();//输出为"经理在工作"
		super.work();//输出为"员工在工作"
		
		System.out.println(name);//窝窝头
		System.out.println(this.name);//窝窝头
		System.out.println(super.name);//一块钱四个 
	}
	/*
	 //super跟this相同,都不能用在静态上下文中
	public static void m2(){
		System.out.println(super.name);
	}
	*/

}

package msuper关键字;

public class Employ {
	
	//Filed,属性
	String name= "窝窝头";
	
	public void work(){
		
		System.out.println("员工在工作");
	}

}

package msuper关键字;

public class Test01 {
	
	public static void main(String[] args){
		
		Manager m = new Manager();
		m.m1();
	}

}

1.2 super用在构造方法中

//父类:账户
package msuper关键字;

public class Account02 {
	
	//Filed
	private String actno;
	private double balance;
	
	//Constructor,作用是创建对象
	public Account02(){
		System.out.println("父类Account的无参数构造方法执行");
	}
	
	public Account02(String actno,double balance){
		
		this.actno=actno;
		this.balance=balance;
		
	}

	
	//setter和getter,作用是重新给变量赋值
	public String getActno() {
		return actno;
	}

	public void setActno(String actno) {
		this.actno = actno;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

}

/*
 *    super关键字用在构造方法中:
 *         语法: super(实参);
 *         
 *       作用:通过子类的构造方法去调用父类的构造方法
 *       语法规则:一个构造方法第一行如果没有显示this(...); 也没有显示super(...);
 *                系统会默认调用super(...);
 *       注意:super(...);的调用只能放在第一行。因此其不能和this(...);共存
 *       
 *       super(...);虽然调用了父类中的构造方法,但并不会创建父类对象
 *       
 *       在java语言中,只要是创建java对象,那么Object中的无参数构造方法一定会执行
 *       
 *       单例模式的缺点:因为其要构造方法私有化,其子类无法创建对象
 *                      所以单例模式的类型无法被继承
 */
package msuper关键字;

public class DebitAccount02 extends Account02{
	
	//Filed
	//自有的独特属性
	private double debit;
	
	//Constructor
	public DebitAccount02(){
		//此处隐藏语句:super(); 括号中无参数即调用父类中的无参数构造方法
		System.out.println("子类DebiAccount02的无参数构造方法执行");
	}
	
	//当属性都私有化时,无法直接访问执行,可以用super(...);
	public DebitAccount02(String actno,double balance,double debit){
		//通过子类的构造方法去调用父类的构造方法,作用是:给当前子类对象中的父类型特征赋值
		super(actno,balance);//其他类的私有化属性不能用this.赋值
		this.debit=debit;
	}
	
	//setter and getter
	public void setDebit(double debit){
		this.debit=debit;
	}
	public double getDebit(){
		return debit;
	}

}

package msuper关键字;

public class Test02 {
	
	public static void main(String[] args){
		
	DebitAccount02 ba = new DebitAccount02();//此局输出结果为:
	                                         // Account的无参数构造方法执行
	DebitAccount02 a1 = new DebitAccount02("452428197525460256",54987564345.00,468946548752.00);
	
	Account02 ac= new Account02();
	
	System.out.println(ac.getActno());
	System.out.println(ac.getBalance());
	
	System.out.println(a1.getActno());
	System.out.println(a1.getBalance());
	
	}
}

2、final关键字

1.1 认识final

package nfinal关键字;

public class Test01 {

}
	/*
	 * final修饰的类无法被继承
	 *
	 * final class A{}

	   class B extends A{}//会报错
	*/

	//final修饰的方法无法被覆盖
	/*
	class A{
		public final void m1(){

		}
	}
	class B extends A(){

	   public void m1(){//Multiple markers at this line
						//- Cannot override the final method from A


		}
	}
	*/

	//final修饰的局部变量,一旦被赋值,不可再改变
	/*
	class A{
		public void m1(){
			//声明
			final int i;

			//第一次赋值
			i =1;

			//不能重新赋值
			i=2;//The final local variable i may already have been assigned

		}
	}
	*/

	//final修饰的成员变量必须"显示的"初始化

	class A{

		//final修饰的成员变量必须手动的初始化
		//final int k;//The blank final field k may not have been initialized
		final int i=100;

		//成员
		final int k;

		//Constructor
		A(){
			k=200;//也就是说final修饰的成员变量必须在调用构造方法之前赋完值
		}
	}


	//final修饰的成员变量一般和static连用
	class MyMath{

		//此时称这样的变量为 常量
		//java规范中要求所有的常量"大写"
		//常量:值不可再改变的量
		public static final double PI=3.14;
	}

2、深入final

/*
 *   深入final
 *   
 *   final修饰的引用类型,该引用不可在重新指向其他的java对象
 *  
 */
package nfinal关键字;

public class Test02 {
	
	public static void main(String[] args){
		
		final Customer c = new Customer("lilei",545);
		
		//c是final的无法重新赋给其新的地址
		//Customer c = new Customer("韩梅梅",45);此处错误
		
		//但是此地址的对象的值是可变的
		c.name="DK";
		c.age=557454787;
		//System.out.println(c);
	}

}

class Customer{
	
	String name;
	double age;
	
	public Customer(String name,double age){
		
		this.name=name;
		this.age=age;
	}
}
posted @ 2021-07-22 22:15  Milen-jie  阅读(42)  评论(0编辑  收藏  举报