我尝试了实现接口。但搜索接口名称为null。请帮忙。 [英] I tried implementing interfaces. But the search for interface name gave null. Please help.

查看:92
本文介绍了我尝试了实现接口。但搜索接口名称为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 语句:从方法中返回值(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 shows null because interfaceName never gets a value: it would get a value of ImplementedInterfaceNames 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 a return statement in your method: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[^]


这篇关于我尝试了实现接口。但搜索接口名称为null。请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆