import java.util.Scanner;
class Dog
{private int size;//利用封装的方法,定义size变量,使得size变量的值始终大于10
public void setsize(int s){
if(s>10)
size=size;
}
public void Bark(){
if(size>60){
System.out.println(size);
System.out.println("Woof!");
}
else if(size>40&&size<=60){
System.out.println(size);
System.out.println("Ruff!");
}
else{
System.out.println(size);
System.out.println("Yip!");
}
}/*编写bark方法,size值>60的时候输出“Woof!”;size值>40的时候输出“Ruff!”;其他时候输出"Yip!"*/
}
public class DogTestDrive
{public static void main(String[] args){
Dog a=new Dog();
Dog b=new Dog();
Dog c=new Dog();//创建至少三个Dog对象
System.out.println("Please input the sizes of three dogs following the order of'a,b,c' individed by','.");
Scanner input=new Scanner(System.in);
String in=input.nextLine();
String[] p=in.split(",");
int[] shu=new int[3];
for (int i=0;i<3 ;i++ )
{shu[i]=Integer.parseInt(p[i]);
}
a.setsize(shu[0]);
b.setsize(shu[1]);
c.setsize(shu[2]);//为每个对象设定或输入size的大小
System.out.printf("the dog a's size of bark is ");
a.Bark();
System.out.printf("the dog b's size of bark is ");
b.Bark();
System.out.printf("the dog c's size of bark is ");//输出每个Dog对象的size大小
c.Bark();
//调用bark方法输出不同size大小的叫声
}
}
第6行 this.size = s 而不是size=size
setSize 里面 size = size了??
不应该是把 s赋值给size吗?