zoukankan      html  css  js  c++  java
  • Java——super关键字

    2.3 super关键字

    ①super不是引用类型,super中存储的不是内存地址,super指向的不是父类对象.

    ②super代表的是当前子类对象中的父类型特征。

    ③什么时候使用super?

     类和父类中都有某个数据,例如,子类和父类中都有name这个属性。如果要再子类中访问父类中的name属性,需要使用  super.

    ④super可以用在什么地方?

    >super可以用在成员方法中,不能用在静态方法中。

    > super可以用在构造方法中。

    ⑤super关键字用在构造方法中:

    语法:

    super(实参);

    作用:通过子类的构造方法去调用父类的构造方法.

    语法规则:一个构造方法第一行如果没有this(...);也没有显示的去调用super(...);系统会默认调用super();

    注意

    >super(...);的调用只能放在构造方法的第一行; super(....)和this(....)不能共存。super(...);调用了父类中的构造方法,但是并不一定会创建父类对象。java语言中只要是创建java对象,那么Object中的无参数构造方法一定会执行。

    >尤其当子父类出现同名成员时,可以用super进行区分;super的追溯不仅限于直接父类; super和this的用法相像,this代表本类对象的引用,super代表父类的内存空间的标识。

    ⑦单例模式缺点:单例模式的类型无法被继承

    super用在成员方法中:

    //员工

    public class Employee{

                String name = "张三";

               

                //成员方法

                public void work(){

                            System.out.println("员工在工作!");

                }

    }

    //经理

    public class Manager extends Employee{

                String name = "李四";

                //子类将父类中的work方法重写了.

                public void work(){

                            System.out.println("经理在工作!");

                }

                //成员方法

                public void m1(){

                            //this.work();

                            //work();

                           

                            super.work();

                           

                            System.out.println(this.name);

                            System.out.println(name);

                            System.out.println(super.name);

                }

                /*

                //this和super相同,都不能用在静态上下文中。

                public static void m1(){

                            System.out.println(super.name);

                }

                */

    }

    super用在构造方法中:

    //账户

    public class Account extends Object{

                //Field

                private String actno;

                private double balance;

                //Constructor

                public Account(){

                            //super();

                            System.out.println("Account的无参数构造方法执行!");

                }

                public Account(String actno,double balance){

                            //super();

                            this.actno = actno;

                            this.balance = balance;

                }

                //setter and getter

                public void setActno(String actno){

                            this.actno = actno;

                }

                public String getActno(){

                            return actno;

                }

                public void setBalance(double balance){

                            this.balance = balance;

                }

                public double getBalance(){

                            return balance;

                }         

    }

    public class DebitAccount extends Account{

                //Field

                //独特属性

                private double debit;

                //Constructor

                public DebitAccount(){

                            //super();

                }

                public DebitAccount(String actno,double balance,double debit){

                            //通过子类的构造方法去调用父类的构造方法,作用是:给当前子类对象中的父类型特征赋值。

                            super(actno,balance);

                           

                            this.debit = debit;

                }

                //setter and getter

                public void setDebit(double debit){

                            this.debit = debit;

                }

                public double getDebit(){

                            return debit;

                }         

    }

    public class Test02{

               

                public static void main(String[] args){

                           

                            DebitAccount da = new DebitAccount();

                           

                }

    }

    单例模式的类无法被继承

    //单例模式的类型没有子类。无法被继承.

    //父类

    public class Servlet{

               

                //构造方法私有

                private Servlet(){}  

    }

    //子类

    class HttpServlet extends Servlet{

    }

     

  • 相关阅读:
    BZOJ 3744 Gty的妹子序列
    BZOJ 3872 Ant colony
    BZOJ 1087 互不侵犯
    BZOJ 1070 修车
    BZOJ 2654 tree
    BZOJ 3243 向量内积
    1003 NOIP 模拟赛Day2 城市建设
    CF865D Buy Low Sell High
    CF444A DZY Loves Physics
    Luogu 4310 绝世好题
  • 原文地址:https://www.cnblogs.com/superjishere/p/11809954.html
Copyright © 2011-2022 走看看