多态 java

来源:百度知道 编辑:UC知道 时间:2024/05/31 09:49:01
import java.io.*;
class Shape{
int x,y;
void getcoord()throws IOException{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the coordinates ");
int x =Integer.parseInt(br.readLine());
int y =Integer.parseInt(br.readLine());
}
void showcoord(){
System.out.print("The x coordinate is "+x+" The y coordinate is"+y);
}
}
class Rect extends Shape
{
/* void getcoord() throws IOException{
super.getcoord();
}
*/ void showcoord()
{
System.out.print("The length is "+x+" The weight is "+y);
}
}
public class Q22 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Shape s =new Rect();
System.out.println("We a

void getcoord()throws IOException{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the coordinates ");
int x =Integer.parseInt(br.readLine());
int y =Integer.parseInt(br.readLine());
}
改为:
void getcoord()throws IOException{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the coordinates ");
this.x =Integer.parseInt(br.readLine());
this.y =Integer.parseInt(br.readLine());
}

你输入的x,y是局部变量,并不是成员变量。showcoord()方法中显示的还是成员变量默认值。