初学者JAVA程序疑问,帮我看看这个程序到底该怎么写。谢谢
publicclassGrandFather{Stringhaircolor;Stringskincolor;inthight;intage=70;staticStrin...
public class GrandFather {
String haircolor;
String skincolor;
int hight;
int age = 70;
static String language = "Chinese";
static void yuyan(String a) {
language = a;
}
public void ageAdd() {
age++;
}
GrandFather() {
String a = null;
haircolor = a;
String b = null;
skincolor = b;
int c = 0;
hight = c;
int d = 0;
age = d;
}
public static void main(String [] args) {
GrandFather father = new GrandFather();
Father son = new Father();
System.out.println(GrandFather.language);
System.out.println(father.skincolor);
System.out.println(father.weightAdd());
System.out.println(son.visible);
System.out.println(son.haircolor);
}
}
class Father extends GrandFather {
int weight = 60;
private void weightAdd() {
weight++;
}
Father() {
super();
int w = 0;
weight = w;
}
}
class Son extends Father {
double visible = 5.0;
Son() {
super();
visible = 5.0;
}
}
帮我看看这个程序到底该怎么写。谢谢 展开
String haircolor;
String skincolor;
int hight;
int age = 70;
static String language = "Chinese";
static void yuyan(String a) {
language = a;
}
public void ageAdd() {
age++;
}
GrandFather() {
String a = null;
haircolor = a;
String b = null;
skincolor = b;
int c = 0;
hight = c;
int d = 0;
age = d;
}
public static void main(String [] args) {
GrandFather father = new GrandFather();
Father son = new Father();
System.out.println(GrandFather.language);
System.out.println(father.skincolor);
System.out.println(father.weightAdd());
System.out.println(son.visible);
System.out.println(son.haircolor);
}
}
class Father extends GrandFather {
int weight = 60;
private void weightAdd() {
weight++;
}
Father() {
super();
int w = 0;
weight = w;
}
}
class Son extends Father {
double visible = 5.0;
Son() {
super();
visible = 5.0;
}
}
帮我看看这个程序到底该怎么写。谢谢 展开
3个回答
展开全部
System.out.println(father.weightAdd());
System.out.println(son.visible); 这两句编译肯定不通过~你是这么定义father是GrandFather的实例 而GrandFather类没有方法weightAdd()
son是Father的实例 而Father类也没有属性visible
那么编译错误
==========================================================
你可以这么改:
public static void main(String [] args) {
Father father = new Father();
Son son = new Son();
System.out.println(GrandFather.language);
System.out.println(father.skincolor);
father.weightAdd();
System.out.println(father.weight);
System.out.println(son.visible);
System.out.println(son.haircolor);
}
}
同时把Father类的 private void weightAdd() {
weight++;
}
修饰符 改成共有的 用于访问
public void weightAdd() {
weight++;
}
============================
PS: 说实话 这个程序的逻辑还有要表达的东西太模糊~ 回去多看看关于继承、多态的实现以及其效果 就好了~
System.out.println(son.visible); 这两句编译肯定不通过~你是这么定义father是GrandFather的实例 而GrandFather类没有方法weightAdd()
son是Father的实例 而Father类也没有属性visible
那么编译错误
==========================================================
你可以这么改:
public static void main(String [] args) {
Father father = new Father();
Son son = new Son();
System.out.println(GrandFather.language);
System.out.println(father.skincolor);
father.weightAdd();
System.out.println(father.weight);
System.out.println(son.visible);
System.out.println(son.haircolor);
}
}
同时把Father类的 private void weightAdd() {
weight++;
}
修饰符 改成共有的 用于访问
public void weightAdd() {
weight++;
}
============================
PS: 说实话 这个程序的逻辑还有要表达的东西太模糊~ 回去多看看关于继承、多态的实现以及其效果 就好了~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询