总结:总是忘记前面的方法:
package com.asdf; public class RR { String name; int age; public RR() { name = "zhangsan"; age = 232; } public RR(String a, int b) { name = a; age = b; } public void print() { System.out.println("我叫:" + name + ",今年:" + age + "岁"); } } class Word extends RR { String color; int classno; String name; // 普通的成员方法都不会了。。操 public void setColor(String color) { this.color = color; } public String getColor() { return color; } public Word() { super("刘德华", 22); name = "周杰伦"; classno = 4; age = 2;// age是父类的属性,它是公共成员变量 // color="whiteS"; } public Word(String name, int age, int classno, String color) { this.color = color; this.name = name; this.age = age; this.classno = classno; }// set是设置属性,get是获取方法属性 public void read() { System.out.println("我的姓名:" + name + ",年龄是:" + age + ",课程编号:" + classno + "父类的成员变量" + super.name + "这颜色是;" + this.getColor()); } public String toString() { return "这是" + color + "颜色"; } } package com.asdf; public class Test { public static void main(String[] args) { Word w = new Word(); RR a = new Word(); a.print(); w.setColor("blue");// 这里传了参数,所以在子类的带参的构造方法里一定要传入参数,否则不能赋值 w.read(); System.out.println(w.toString()); // System.out.println(w.setColor("blue")); // 这里多态为什么不能调用子类的方法 // ((Word)a).read(); // System.out.println("这是普通的方法"+w.color); // System.out.println(a.toString()); /* * w.print(); w.read(); System.out.println(w.toString()); */ } }