Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用值实例化接口类型变量_Java - Fatal编程技术网

Java 用值实例化接口类型变量

Java 用值实例化接口类型变量,java,Java,在我只做了1小时关于接口的课程后,我接受了一些任务作为家庭作业,但这给了我真正的问题: 一个接口I1具有:方法:intread() 一个类A实现该接口并包含:从键盘读取值的方法int read(); B类包含:一个变量c类型I1,一个具有一个参数的构造函数,用于为c赋值,一个方法afis(),显示数字的总和 这就是我到目前为止所做的,但是我被困在这里,我如何才能给c赋值,并将其与a类中的n1相加 package toDo2; import javax.swing.JOptionPane; p

在我只做了1小时关于接口的课程后,我接受了一些任务作为家庭作业,但这给了我真正的问题: 一个接口
I1
具有:方法:
intread()
一个类
A
实现该接口并包含:从键盘读取值的方法
int read()
; B类包含:一个变量
c
类型
I1
,一个具有一个参数的构造函数,用于为
c
赋值,一个方法
afis()
,显示数字的
总和

这就是我到目前为止所做的,但是我被困在这里,我如何才能给
c
赋值,并将其与
a
类中的
n1
相加

package toDo2;

import javax.swing.JOptionPane;

public interface i1 {
    int read();
}

class A implements i1 {

    public int read() {
        int n1 = Integer.parseInt(JOptionPane.showInputDialog("Introduce the value for n1"));
        return n1;
    }

}

class B {

    int sum;
    i1 c;

    B(i1 c) {

    }   
}

class toDo2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        B n1 = new B(/*here must be the value of c which is i1 type*/);



    }

}

看来,您需要在main方法中创建类
A
的实例,因为它是迄今为止唯一实现接口
i1
的类:

class B {
    int sum;
    i1 c;

    B(i1 c) {
        this.c = c;
    }
}

class toDo2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        B n1 = new B(new A());
        // ...
    }
}
另外,类B将不知道其
c
字段的具体实现-它将能够调用接口中定义的方法
int read()

这样就可以了

B n1 = new B(new A());
基本上,您的
class A
属于
I1
类型,因为它实现了
I1

给你:

import java.util.Scanner;

public class Main{

     public static void main(String []args){
        I1 a = new A();
        a.read(); //this call will initiate taking input
        B n1 = new B(a);
        int sum = n1.sum(a.getFirstNumber(), a.getSecondNumber());
        System.out.println(sum);
     }
}

interface I1 {
    void read();
    int getFirstNumber();
    int getSecondNumber();
}

class A implements I1 {
    int n1;
    int n2;
    public void read() {
        Scanner in = new Scanner(System.in);
        n1 = in.nextInt();
        n2 = in.nextInt();
    }

    public int getFirstNumber() {
        return n1;
    }

    public int getSecondNumber() {
        return n2;
    }

}

class B {

    I1 c;

    B(I1 c) {
        this.c = c;
    }

    public int sum(int n1, int n2)  {
        return n1+n2;
    }
}

这令人困惑。main是什么样子的,这样我们就可以看到您想要如何使用这些类了?main现在是空的。将实例化B的Main类。这是Main()的唯一任务。我认为您将在B构造函数中将此.c分配给c。但是,由于我看不到主视图,我不知道您将如何处理B。好的,我现在就添加主视图,但由于答案和注释中显示的不清楚,它已空启动以关闭。谢谢,
bn1=new B(new A())有效,但我必须在
B
课上求和,2个数字之间的和,我猜
c
不会是一个数字,我无法计算出scound数字是什么。似乎你需要和你的老师澄清这项任务,并了解哪些数字需要在家庭作业中求和。谢谢,这就像我对u下的另一个好人说的,但我必须在B类中做两个数字的和,如果我做了和其他int read,那么A类中的一个无论如何都不会被称为感谢,感谢你的努力,但我必须100%尊重任务,所以只需要int read()在接口中,只需在类A中调用int read…我认为这个平台是用于帮助而不是用于“100%任务”,您可以根据需要调整它。你已经得到了帮助。