zoukankan      html  css  js  c++  java
  • 继承与重写的具体事例

    package com.hanqi.maya.test;                         这是父类在应用这个父类时应先将包名改成自己设置的包名。
    
    public class Hero {
      private  String hname;
      private  String carrytype;
      
      public Hero() {}
    
    	public Hero(String hname, String carrytype) {
    		super();
    		this.hname = hname;
    		this.carrytype = carrytype;
    }
       public void print() {
    	   System.out.println("这个英雄名字是" + hname);
    	   System.out.println("这个英雄输出属性是" + carrytype);
       }
    
    	public String getHname() {
    		return hname;
    	}
    
    	public void setHname(String hname) {
    		this.hname = hname;
    	}
    	
    	public String getCarrytype() {
    		return carrytype;
    	}
    	
    	public void setCarrytype(String carrytype) {
    		this.carrytype = carrytype;
    	}
    
    	
      
    }
    

      

    package com.hanqi.maya.test;
    
    public class ADHero extends Hero {
        private String hometown;
        public ADHero() {}
    	public ADHero(String hname,String carrytype,String hometown) {
    		super(hname,carrytype);
    		this.hometown = hometown;
    	}
    	 public void print() {
    		   System.out.println("这个英雄名字是" + super.getHname());
    		   System.out.println("这个英雄输出属性是" + super.getCarrytype());
    		   System.out.println("这个英雄属于哪个阵营"+hometown);
    	   }
    	public String getHometown() {
    		return hometown;
    	}
    	public void setHometown(String hometown) {
    		this.hometown = hometown;
    	}
    	 
    }
    

      

    package com.hanqi.maya.test;
    
    public class APHero extends Hero{
    	private String hometown;
    	public APHero(){}
    	public APHero(String hname,String carrytype,String hometown) {
    		super(hname,carrytype);
    		this.hometown = hometown;
    	}
    	
    	public void print() {
    		 System.out.println("这个英雄名字是" + super.getHname());
    		 System.out.println("这个英雄输出属性是" + super.getCarrytype());
    		 System.out.println("这个英雄属于哪个阵营"+hometown);
    	}
    	public String getHometown() {
    		return hometown;
    	}
    	public void setHometown(String hometown) {
    		this.hometown = hometown;
    	}
    }
    	   
    

      两个子类继承了父类里的hname和carrytype但同样都属于自己的特性!

  • 相关阅读:
    牛客练习赛71 F-红蓝图 (kruskal重构树 + 线段树合并)
    2020杭电多校第一场 Finding a MEX
    Codeforces 235C Cyclical Quest (后缀自动机)
    HDu6583 Typewriter (后缀自动机 + dp)
    2020牛客暑期多校训练营(第八场)A All-Star Game
    HDu4416 Good Article Good sentence (后缀自动机)
    icpc小米 A . Intelligent Warehouse
    计数类dp
    主席树
    博弈论
  • 原文地址:https://www.cnblogs.com/sunbo123/p/7928774.html
Copyright © 2011-2022 走看看