【问题标题】:how to call a if-Statement from another class如何从另一个类调用 if 语句
【发布时间】:2018-06-13 07:22:50
【问题描述】:

在这个程序中出现了 if 语句,它必须从另一个类名为 Studentfactory 的类中调用。

public class Control {
    public static void main(String[] args) {
        Student[] studs = new Student[3];
        for (int i = 0; i < 3; i++) {
            studs[i] = createStudent();
        }
        for (int i = 0; i < 3; i++) {
            System.out.println(studs[i]);
        }
    }

    static Student createStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name:");
        String name = sc.nextLine();
        System.out.print("Enter your age:");
        int age = sc.nextInt();

        if(age <20) {
            return new JuniorStudent(name, age);
        } else if( age < 30) {
            return new IntermediateStudent(name,age);
        }
        return new SeniorStudent(name, age);
    }
}

// if 语句必须从这个类中调用

package demo;

public class Studentfactory {



    }

【问题讨论】:

  • 你调用方法,而不是ifs。你到底想做什么?
  • 你想从另一个类中调用什么“if语句”?
  • 我完全不明白你想做什么。请更准确地描述它。
  • sry 先生如何通过另一个类的方法调用 if 语句
  • 您要调用哪个if 语句以及从哪里调用?

标签: java function if-statement methods overriding


【解决方案1】:

“if 语句”的 interface 将是 Predicate,在您的情况下可能是 Predicate&lt;Integer&gt;。你可以做类似的事情

class AgePredicates {
    public static final Predicate<Integer> isJunior = a -> a < 20;
    public static final Predicate<Integer> isIntermediate = a -> a > 20 && a < 30;
    public static final Predicate<Integer> isSenior = a -> a >= 30;
}

然后“从不同的班级呼叫他们”

if (AgePredicates.isJunior.test(age)) {
    // ...
} else if (AgePredicates.isIntermediate.test(age)) {
    // ...
} else {
    // ...
}

【讨论】:

  • 虽然这可能可行,但 OP 似乎并不清楚类和方法是如何工作的。在这一点上,谓词可能是一个过于高级的概念。
  • 兄弟我不想在同一个班级使用
  • @jhamon 我同意,我已经通过使用enum 而不是classstatic finals 来淡化我的答案。但是,这是最接近我对问题的理解的答案;)
【解决方案2】:

如果我理解正确,你想从类StudentFactory调用类Control的静态方法createStudent

这样做的方法是

Control.createStudent()

(在课堂上StudentFactory

希望这会有所帮助!

【讨论】:

  • 先生,你能简单地做一下吗
  • 就是上面显示的一行代码。如果它不起作用,可能是因为您的 StudentFactory.java 与 Control.java 位于不同的包中。这些类在同一个包中吗?
【解决方案3】:

您可以做的是在您的方法中将Student 的年龄作为参数:

static Student createStudent(int age) {  // note the parameter here
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter your name:");
    String name = sc.nextLine();

    // no sc.nextInt() here anymore
    if(age <20) {
        return new JuniorStudent(name, age);
    } else if( age < 30) {
        return new IntermediateStudent(name,age);
    }
    return new SeniorStudent(name, age);
}

通过这样做,您可以从其他地方的命令行中读取下一个整数,然后使用下一个 int 作为参数调用该方法。然后方法决定要创建什么样的学生。

更新

在您提供的其他课程中,您可以从任何地方(很可能是命令行)获取年龄,然后将其用作如下参数:

public class Studentfactory {

    public Student createStudentByControl() {
        // create a new Control object
        Control control = new Control();
        // get the age from command line
        int age = sc.nextInt();
        // use the age as parameter for the control to create a new student
        Student student = control.createStudent(age);
        return student;
    }
}

【讨论】:

  • 先生,我想从不在同一个班级的另一个班级进行 if 语句
  • 这可以通过在另一个类中实例化一个新的Control 然后通过control.createStudent(20) 调用它的方法来实现。
  • tq 先生,但我是初学者,你能简单介绍一下程序吗
  • 请编辑您的问题并添加您想要在不同学生之间使用决定的班级代码。
猜你喜欢
  • 2022-01-06
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多