我尝试了实现接口。但搜索接口名称为null。请帮忙。 [英] I tried implementing interfaces. But the search for interface name gave null. Please help.
本文介绍了我尝试了实现接口。但搜索接口名称为null。请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.util。*;
public class TestClass {
public static String interfaceName;
public static void main ( String [] args){
MyCalculator my_calculator = new MyCalculator();
Scanner nw = new Scanner(System.in);
int no = nw.nextInt();
System.out.println( 我实现了 + interfaceName);
System.out.print(my_calculator.divisor_sum(no));
}
静态 void ImplementedInterfaceNames(Object o){
Class [] theInterfaces = o.getClass()。getInterfaces();
for ( int i = 0 ; i< theInterfaces.length; i ++){
interfaceName = theInterfaces [i] .getName();}
}
}
interface AdvancedArithmatic {
int divisor_sum( int N);
}
class MyCalculator implements AdvancedArithmatic {
@覆盖
public int divisor_sum( int n){
int mySum = 0;
for ( int i = 1; i< = n; i ++){
if (n%i == 0)mySum + = i;
}
return mySum;
}
}
我的尝试:
import java.util。*;
public class TestClass {
public static String interfaceName;
public static void main ( String [] args){
MyCalculator my_calculator = new MyCalculator();
Scanner nw = new Scanner(System.in);
int no = nw.nextInt();
System.out.println( 我实现了 + interfaceName);
System.out.print(my_calculator.divisor_sum(no));
}
静态 void ImplementedInterfaceNames(Object o){
Class [] theInterfaces = o.getClass()。getInterfaces();
for ( int i = 0 ; i< theInterfaces.length; i ++){
interfaceName = theInterfaces [i] .getName();}
}
}
interface AdvancedArithmatic {
int divisor_sum( int N);
}
class MyCalculator implements AdvancedArithmatic {
@覆盖
public int divisor_sum( int n){
int mySum = 0;
for ( int i = 1; i< = n; i ++){
if (n%i == 0)mySum + = i;
}
return mySum;
}
}
解决方案
显示null
因为interfaceName
永远不会得到一个值:它会得到一个值ImplementedInterfaceNames
会被调用,但它永远不会被调用。
此外,不是使用静态变量来为方法提供输出值,最好使用return $ c您的方法中的$ c>语句:从方法中返回值(Java™教程>学习Java语言>类和对象) [ ^ ]
import java.util.*;
public class TestClass {
public static String interfaceName;
public static void main(String[] args) {
MyCalculator my_calculator=new MyCalculator();
Scanner nw=new Scanner(System.in);
int no=nw.nextInt();
System.out.println("I implemented "+interfaceName);
System.out.print(my_calculator.divisor_sum(no));
}
static void ImplementedInterfaceNames(Object o){
Class[] theInterfaces = o.getClass().getInterfaces();
for (int i = 0; i < theInterfaces.length; i++){
interfaceName = theInterfaces[i].getName();}
}
}
interface AdvancedArithmatic{
int divisor_sum(int n);
}
class MyCalculator implements AdvancedArithmatic{
@Override
public int divisor_sum(int n) {
int mySum=0;
for(int i=1;i<=n;i++){
if(n%i==0) mySum+=i;
}
return mySum;
}
}
What I have tried:
import java.util.*;
public class TestClass {
public static String interfaceName;
public static void main(String[] args) {
MyCalculator my_calculator=new MyCalculator();
Scanner nw=new Scanner(System.in);
int no=nw.nextInt();
System.out.println("I implemented "+interfaceName);
System.out.print(my_calculator.divisor_sum(no));
}
static void ImplementedInterfaceNames(Object o){
Class[] theInterfaces = o.getClass().getInterfaces();
for (int i = 0; i < theInterfaces.length; i++){
interfaceName = theInterfaces[i].getName();}
}
}
interface AdvancedArithmatic{
int divisor_sum(int n);
}
class MyCalculator implements AdvancedArithmatic{
@Override
public int divisor_sum(int n) {
int mySum=0;
for(int i=1;i<=n;i++){
if(n%i==0) mySum+=i;
}
return mySum;
}
}
解决方案
It showsnull
becauseinterfaceName
never gets a value: it would get a value ofImplementedInterfaceNames
would get called, but it never gets called.
Also, instead of using a static variable for a method to give its output values, it's better practice to use areturn
statement in your method: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[^]
这篇关于我尝试了实现接口。但搜索接口名称为null。请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文