zoukankan      html  css  js  c++  java
  • JAVA之继承的必要性

    //说明继承的必要性
    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中有关于反射的皮毛----自己的简略认知
    在java中异常中的题目---重要的一点
    在一个陌生的环境里学习新的-----单例
    在java开发环境中,快捷键的使用及用法
    指针(一)
    #ifdef、#ifndef、#else、#endif执行条件编译
    oc中的数组
    控制循环结构
    oc中的枚举
    oc中类的实例化及方法调用
  • 原文地址:https://www.cnblogs.com/milantgh/p/4036128.html
Copyright © 2011-2022 走看看