小弟初接触Java,请各位高手帮我修改下,
class Animals
{
private String color;
private int high;
Animals(){
}
Animals(String color,int high){
this.color = color;
this.high = high;
}
public void info(Animals a){
System.out.println("color="+a.color);
if (a instanceof Bird)
{
Bird bird = (Bird) a;
System.out.println("name="+bird.name+" high="+bird.high);
}
else
System.out.println("I'd not konw who is this");
}
}
class Bird extends Animals
{
private String name;
Bird(){
}
Bird(String name,String color,int high){
super(color,high);
this.name = name;
}
}
public class FatherSub
{
public static void main(String arg[]){
Animals a = new Animals();
Bird b = new Bird("john","blue",12);
a.info(b);}
}
错误提示:FatherSub.java:28: name has private access in Bird
System.out.println("name="+bird.name+" high="+bird.high);
^
FatherSub.java:28: high has private access in Animals
System.out.println("name="+bird.name+" high="+bird.high);
^
2 errors
class Animals
{
private String color;
private int high;
Animals(){
}
Animals(String color,int high){
this.color = color;
this.high = high;
}
public void info(Animals a){
System.out.println("color="+a.color);
if (a instanceof Bird)
{
Bird bird = (Bird) a;
System.out.println("name="+bird.name+" high="+bird.high);
}
else
System.out.println("I'd not konw who is this");
}
}
class Bird extends Animals
{
private String name;
Bird(){
}
Bird(String name,String color,int high){
super(color,high);
this.name = name;
}
}
public class FatherSub
{
public static void main(String arg[]){
Animals a = new Animals();
Bird b = new Bird("john","blue",12);
a.info(b);}
}
错误提示:FatherSub.java:28: name has private access in Bird
System.out.println("name="+bird.name+" high="+bird.high);
^
FatherSub.java:28: high has private access in Animals
System.out.println("name="+bird.name+" high="+bird.high);
^
2 errors
解决方案 »
- 这种for循环你见过么 求解!~~~~
- Java泛型的接口编程问题
- 将java项目转换成.net项目,或者将jar文件转换成exe文件,使其直接在windows系统下运行
- 关于unicode的一个问题
- 面试题!!!50分
- JBuilder开发的程序能否在Linux,Unix上运行?
- 一个菜鸟关于J2EE环境变设置的问题
- 我写的applet,在jbuilder下运行没问题,但在jdk下用appletviewer看就提示出错?
- 关于s1.compareTo(s2)和s2.compareTo(s1)的区别
- 谁能给提供一些servlet绘制动态统计图的休息呀。
- log4j与换行,跨平台
- tomcat怎么配置啊?谢谢各位啦!
可以在Bird定义一个public String getName() {return name}然后在需要用到Bird的name的地方用getName返回就好
FatherSub.java:28: high has private access in Animalsprivate改public 或者用楼上的
package test;class Animals{
private String color;
private int high;
Animals(){}
Animals(String color,int high){
this.color = color;
this.setHigh(high);
}
public void info(Animals a){
System.out.println("color="+a.color);
if (a instanceof Bird){
Bird bird = (Bird) a;
System.out.println("name="+bird.getName()+" high="+bird.getHigh());
}else
System.out.println("I'd not konw who is this");
}
public void setHigh(int high) {
this.high = high;
}
public int getHigh() {
return high;
}
}class Bird extends Animals{
private String name;
Bird(){}
Bird(String name,String color,int high){
super(color,high);
this.setName(name);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}public class FatherSub{
public static void main(String arg[]){
Animals a = new Animals();
Bird b = new Bird("john","blue",12);
a.info(b);
}
}
private String color;
protected int high; Animals() {
} Animals(String color, int high) {
this.color = color;
this.high = high;
} public void info(Animals a) {
System.out.println("color=" + a.color);
if (a instanceof Bird) {
Bird bird = (Bird) a;
System.out.println("name=" + bird.name + " high=" + bird.high);
} else
System.out.println("I'd not konw who is this");
}
}class Bird extends Animals {
String name; Bird() {
} Bird(String name, String color, int high) {
super(color, high);
this.name = name;
}}public class FatherSub {
public static void main(String arg[]) {
Animals a = new Animals();
Bird b = new Bird("john", "blue", 12);
a.info(b); }
}