码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA之继承的必要性

时间:2014-10-20 00:39:36      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:ar   java   sp   bs   new   as   对象   return   方法   

//说明继承的必要性
package com.test;

public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //小学生对象
        
        Pupil pl = new Pupil();
        
        //中学生对象
        
        MiddleStu ms =  new MiddleStu();
        
        //大学生对象
        
        CollegeStu cs = new CollegeStu();
        
        
        //设置fee
        
        pl.pay(100);
        
        ms.pay(500);
        
        cs.pay(1000);
        
        //打印设置的fee属性
        
        System.out.println(pl.printFee());
        
        System.out.println(ms.printFee());
        
        System.out.println(cs.printFee());
        
    }
}
    
    //这里的方法不能为public类型
    
    class Stu{
        //定义成员属性
        public int age;

        public String name;

        public float fee;
        
        public float printFee()
        {
            return fee;
        }
    }
    
    //小学生
    
    class Pupil extends Stu{
        
        
                //缴费
                public void pay(float fee)
                {
                    this.fee = fee;
                }
    }
    
    //中学生
    
    class MiddleStu extends Stu{
        
                
                //缴费
                public void pay(float fee)
                {
                    this.fee = fee*0.8f;
                }
    }
    
    //大学生
    
    class CollegeStu extends Stu{
        
                //缴费
                public void pay(int fee)
                {
                    this.fee = fee*0.1f;
                }
    }

JAVA之继承的必要性

标签:ar   java   sp   bs   new   as   对象   return   方法   

原文地址:http://www.cnblogs.com/milantgh/p/4036128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!