栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java基础知识15:方法覆写,属性覆盖

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java基础知识15:方法覆写,属性覆盖

hhhhhhhhhh

wake and see 

package com.hiyo.HighClass;


class A {
    public String ID = "CLASS A";
    private String name ;
    private float price ;
    public A(String name,float price){
        this.setName(name);
        this.setPrice(price);
    }//父类中带参数的构造方法
    public String getName(){
        return this.name ;
    }
    public float getPrice(){
        return this.price ;
    }
    public void setName(String name) {
        this.name = name ;
    }
    public void setPrice(float price){
        this.price = price ;
    }
    public void print(){
        System.out.println("这个是父类中的打印方法");
        System.out.println(this.getName() + this.getPrice());
    }
    private void see(){
        System.out.println("父类中的私有方法see");
    }
    public void watch() {
        this.see();
    }
}

class B extends A {
    public B(String name, float price){
        super(name,price);
    }
    //super()或者super(a,b)可以调用父类中的无参数和有参数的方法。
    public String ID = "CLASS B";

    
    public void print(){//覆写父类中的同名方法
        
        

        System.out.println("这是子类覆写的方法");
        System.out.println(this.getName() + this.getPrice());
    }
    
    public void see(){
        System.out.println("子类中的SEE方法,访问default");
    }
    
    public void IDPrint() {
        System.out.println(super.ID);
        
        System.out.println(this.ID);
    }
}
public class ExtendsDemo3 {
    public static void main(String[] args ) {
        A a = new B("a",5.0f) ;
        a.print();//子类的print
        A a1 = new A("b",6.0f) ;
        a1.print(); //父类的print
        B b = new B("x",7.0f) ;
        b.watch();
        b.IDPrint();
    }

}




下面是一个Java的实例

package com.hiyo.HighClass;

class Array {
    private int temp[] ;//定义整型数组,大小外部确定
    private int foot ; //数组添加的角标
    public Array(int len){//构造函数
        if(len>0){
            this.temp = new int[len];//根据传入的大小开辟空间
        } else {
            this.temp = new int[1];//最小又一个元素
        }
    }
    public boolean add(int i) {
        if(this.foot 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356230.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号