黑马程序员技术交流社区
标题:
dogs[i]=new Dog();提示一个静态的函数不能调用一非静态的方法,怎么改?求解答
[打印本页]
作者:
李金伦
时间:
2015-7-7 23:55
标题:
dogs[i]=new Dog();提示一个静态的函数不能调用一非静态的方法,怎么改?求解答
import java.util.*;
public class SdutentAarryTest {
public static void main(String[] args) {
Dog dogs[]=new Dog[4];
for(int i= 0;i<4;i++){
dogs[i]=new Dog();
System.out.println("输入第"+(i+1)+"狗名字");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
dogs[i].setName(name);
System.out.println("输入第"+(i+1)+"狗的重量");
String s_weight=scan.nextLine();
float weight=Float.parseFloat(s_weight);//s
dogs[i].setWeight(weight);
}
float allweight=0;
for(int i=0;i<4;i++){
allweight+=dogs[i].getWeight();
}
System.out.print("总体重="+allweight+"平均体重等于="+allweight/4);
}
class Dog{
private float weight;
private String name;
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
作者:
夜写意
时间:
2015-7-8 20:08
哈哈!发现李金伦!!! 你的 Dog 这个类是内部类,所以要创建对象需要用 new SdutentAarryTest().new Dog(); 来创建。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2