std::vector unsigned char buf;
outputFile.write( buf[0], buf.size() );
final 和static 的方法不能被重写 但是静态的方法可以被重新定义
你那个不是重写 是重新定义 重新定义不属于多态范畴
所以他只看引用类型 但是java也强烈建议你千万不要用引用去访问静态变量或静态方法,因为那样你会逻辑混乱
你用的f其实就是子类型,根本没有多态的,就算是调用重写的非静态方法还是体现不出多态性
那么其调用的grow一定是重新定义的子类方法
然而
Amphibian.grow(f);
来说 你在grow参数里声明的是父类的引用 但是你却把一个子类实例传递进来
多态性在这体现了 首先调用的是父类的静态没疑问了 然而a.eat();出现了多态行为
父类引用指向子类对象 调用的是子类重写的方法
代码一不是重写
重写规则是
参数列表必须与被重写方法的相同
返回类型必须与被重写方法的返回类型相同
代码一中 public void method()和 public float method(String s)参数列表不同,所以不是重写
代码 二中public float getNum() 和 public void getNum() 返回类型不同,所以报错
在Java中如何来定义重写:Java程序中类的继承特性可以产生一个子类,子类继承父类就拥有了父类的非私有的属性(方法和变量),在子类中可以增加自己的属性(方法和变量),同时也可以对父类中的方法进行扩展,以增强自己的功能,这样就称之为重写,也称为复写或者覆盖.所谓方法重写就是子类的方法和父类中继承下来的方法具有完全相同的方法名、返回值类型、方法的参数个数以及参数类型,这样才能被称为方法重写.
代码体现:
// 这是父类的定义
public class Person {
public void eat() {
System.out.println("=====这是父类Person的eat方法=======");
}
// 这是子类的定义
public class Student extends Person {
@Override
System.out.println("===这是子类Student的eat方法===");
// main方法测试
public static void main(String[] args) {
Student student = new Student();
student.eat(); //输出:===这是子类Student的eat方法===
子类重写父类的方法后,在实例化子类的时候则调用的是子类中的方法,父类的方法就如同被覆盖了一样.如果在子类中需要调用父类的方法,则在子类方法中使用super关键字调用父类的方法,格式:super.父类中的方法名.
方法重写就是子类的方法覆盖了父类的方法,即子类的方法名和父类的方法名相同
class People
{
protected double weight,height;
public void speakHello()
System.out.println("yayawawa");
public void averageHeight()
System.out.println("average height:"+height);
public void averageWeight()
System.out.println("average weight:"+weight);
class ChinaPeople extends People
//代码1//重写public void speakHello()方法
// 要求输出类似"土嘎嘎的粉丝们大家好,吃饭了吗"这样的汉语信息
System.out.println("土嘎嘎的粉丝们大家好,吃饭了吗");
//System.out.println(this.speakHello()+this.averageHeight()+this.averageWeight());
public void chinaGongfu()
System.out.println("坐如钟,站如松,睡如弓");
class AmericanPeople extends People
//要求输出类似"How do you do"这样的英语信息
System.out.println("How do you do");
public void americanBoxing()
System.out.println("直拳,勾拳");
class BeijingPeople extends ChinaPeople
//要求输出类似"您好"这样的汉语信息
System.out.println("您好!");
//代码10//重写public void averageHeight()方法
//代码11//重写public void averageWeight()方法
public void beijingOpera()
System.out.println("京剧术语");
public class Example
public static void main(String args[])
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
beijingPeople.beijingOpera();
americanPeople.americanBoxing();
beijingPeople.chinaGongfu();
以上就是土嘎嘎小编为大家整理的重写java代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!