当前位置: 移动技术网 > IT编程>开发语言>Java > Java自学-接口与继承 super

Java自学-接口与继承 super

2019年09月14日  | 移动技术网IT编程  | 我要评论

亚洲龙腾小说阅读网,h小游戏bt,玖恩青春定格原液

java的super关键字

步骤 1 : 准备一个显式提供无参构造方法的父类

准备显式提供无参构造方法的父类
在实例化hero对象的时候,其构造方法会打印
“hero的构造方法 "

package charactor;
 
import property.item;
 
public class hero {
        
    string name; //姓名
        
    float hp; //血量
        
    float armor; //护甲
        
    int movespeed; //移动速度
     
    public void useitem(item i){
        system.out.println("hero use item");
        i.effect();
    }
     
    public hero(){
        system.out.println("hero的构造方法 ");
    }
     
    public static void main(string[] args) {
        new hero();
    }
      
}

步骤 2 : 实例化子类,父类的构造方法一定会被调用

实例化一个adhero(), 其构造方法会被调用
其父类的构造方法也会被调用
并且是父类构造方法先调用
子类构造方法会默认调用父类的 无参的构造方法
实例化子类,父类的构造方法一定会被调用

package charactor;
  
public class adhero extends hero implements ad{
  
    @override
    public void physicattack() {
        system.out.println("进行物理攻击");
    }
     
    public adhero(){
         
        system.out.println("ad hero的构造方法");
    }
     
    public static void main(string[] args) {
 
        new adhero();
         
    }
  
}

步骤 3 : 父类显式提供两个构造方法

分别是无参的构造方法和带一个参数的构造方法

package charactor;
 
import property.item;
 
public class hero {
        
    string name; //姓名
        
    float hp; //血量
        
    float armor; //护甲
        
    int movespeed; //移动速度
     
    public void useitem(item i){
        system.out.println("hero use item");
        i.effect();
    }   
     
    public hero(){
        system.out.println("hero的无参的构造方法 ");
    }
     
    public hero(string name){
        system.out.println("hero的有一个参数的构造方法 ");
        this.name = name;
    }
     
    public static void main(string[] args) {
        new hero();
    }
      
}

步骤 4 : 子类显式调用父类带参构造方法

使用关键字super 显式调用父类带参的构造方法

package charactor;
  
public class adhero extends hero implements ad{
  
    @override
    public void physicattack() {
        system.out.println("进行物理攻击");
    }
     
    public adhero(string name){
        super(name);
        system.out.println("ad hero的构造方法");
    }
     
    public static void main(string[] args) {
        new adhero("德莱文");
    }
  
}

步骤 5 : 调用父类属性

通过super调用父类的movespeed属性
adhero也提供了属性movespeed

public int getmovespeed(){
   return this.movespeed;
}
 
public int getmovespeed2(){
   return super.movespeed;
}
package charactor;
  
public class adhero extends hero implements ad{
 
    int movespeed=400; //移动速度
 
    @override
    public void physicattack() {
        system.out.println("进行物理攻击");
    }
     
    public int getmovespeed(){
        return this.movespeed;
    }
     
    public int getmovespeed2(){
        return super.movespeed;
    }
     
    public static void main(string[] args) {
        adhero h= new adhero();
         
        system.out.println(h.getmovespeed());
        system.out.println(h.getmovespeed2());
         
    }
  
}

步骤 6 : 调用父类方法

adhero重写了useitem方法,并且在useitem中通过super调用父类的useitem方法

package charactor;
 
import property.item;
import property.lifepotion;
 
public class adhero extends hero implements ad {
 
    int movespeed = 400; // 移动速度
 
    @override
    public void physicattack() {
        system.out.println("进行物理攻击");
    }
 
    public int getmovespeed() {
        return this.movespeed;
    }
 
    public int getmovespeed2() {
        return super.movespeed;
    }
 
    // 重写useitem,并在其中调用父类的useritem方法
    public void useitem(item i) {
        system.out.println("adhero use item");
        super.useitem(i);
    }
 
    public static void main(string[] args) {
        adhero h = new adhero();
 
        lifepotion lp = new lifepotion();
 
    }
 
}

练习

父类hero提供了一个有参的构造方法:

public hero(string name){
  this.name = name;
}

但是没有提供无参的构造方法
子类应该怎么处理?

package charactor;
   
public class hero {
    public string name;
    protected float hp;
   
    public hero(string name){
        this.name = name;
    }
     
//    故意不提供无参的构造方法
//    public hero(){
//     
//    }
     
    public static void main(string[] args) {
     
    }
       
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网