---
title: "父类引用子类对象"
author: "Java-feifeiok"
date: "2009-08-30T14:09:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-08-30T14:09:00Z"
id: 464744
url: "https://java-feifeiok.iteye.com/blog/464744"
---

父类引用子类对象

**有什么错误欢迎大家指正**

package test;

class Fruit1 {  
static String color = "黄色";  
String size="大";

static String get(){  
return color;  
}

String get1(){  
return size;  
}  
}  
/\**  
\* 父类引用子类对象  
\* 如果父类的方法可以被重写(静态方法不能被重写，  
\* 因为它是随着类的创建而存在于内存，只有一份)，  
\* 并且已经重写，那么调用的是子类的方法  
\* 反之，则调用父类里的方法  
\*/

public class Fruit extends Fruit1{  
static String acolor = "lv";  
String asize="x";  
// 比较特殊，表面上即不能算是重写，但是又不能去掉static(又算是重写···)  
// (静态方法不能被重写，  
// 因为它是随着类的创建而存在于内存，只有一份)  
static String get(){  
return acolor;  
}  
@Override  
String get1() {  
return asize;  
}

String getm(){  
return "must";  
}

public static void main(String\[] args) {  
Fruit1 f = new Fruit();

System.out.println(f.get());  
System.out.println(f.get1());  
}  
}