没有静态变量就无法运行

时间:2018-11-07 07:03:55

标签: java

> `package class_practice;

public class friend1 {
    public static void main (String [] args) {
        Friend friend1=new Friend();
        Friend friend2=new Friend();
        friend2.setName("ANN");
        friend1.setName("ANNA");
        friend2.setAge(18);
        friend1.setAge(19);
        friend2.setHeight(167.8);
        friend1.setHeight(166);
        friend2.setWeight(55.5);
        friend1.setWeight(53.3);
        System.out.println(friend1.getName(friend1)+"\n"+friend1.getAge(friend1)+"\n"+friend1.getHeight(friend1)+"\n"+friend1.getWeight(friend1)
        +friend2.getName(friend2)+"\n"+friend2.getAge(friend2)+"\n"+friend2.getHeight(friend2)+"\n"+friend2.getWeight(friend2));

    }
    static class Friend{
        //don't want to insert static 
        private String name;
        private int age;
        private int number;
        private double height;
        private double weight;

        Friend(){
            final String NAME="undefined";
            final int AGE=0;
            final int NUMBER=0;
            final double HEIGHT= 0.0;
            final double WEIGHT= 0.0;

            name=NAME;
            age=AGE;
            height=HEIGHT;
            weight=WEIGHT;
        }

        public static boolean setName(String name1) {
            if(name1.length()<0||name1.length()>20)
                return false;
            name=name1;
            return true;
        }
        public static boolean setAge(int age1) {
            if(age1<0||age1>150)
                return false;
            age=age1;
            return true;
        }
        public static boolean setNumber(int number1) {
            number=number1;
            return true;
        }   
        public static boolean setHeight(double height1) {
            if (height1<100.0)
                return false;
            height=height1;
            return true;
        }
        public static boolean setWeight(double weight1) {
            if (weight1<20.0)
                return false;
            weight=weight1;
            return true;
        }
        public String getName(Friend friend) {
            return friend.name;
        }
        public int getAge(Friend friend) {
            return friend.age;
        }
        public int getNumber(Friend friend) {
            return friend.number;
        }
        public double getHeight(Friend friend) {
            return friend.height;
        }
        public double getWeight(Friend friend) {
            return friend.weight;
        }
    }
}

嗨,我是Java初学者! 我不想将friend.name设为静态,因为我想存储其他值。但是代码不会让我运行它。它说唯一的解决方案是使名称静态。有时它会起作用,但有时我不会不知道自己在做错什么:(有人可以帮我解决问题吗?

0 个答案:

没有答案