English 中文(简体)
Java没有检测类吗?
原标题:Java not detecting class?
  • 时间:2012-05-26 13:51:05
  •  标签:
  • java

所以,我对爪哇来说是相当新奇的。我学到了很多关于新事物的知识。但是...我当然不理解一切。

我有两门课,一个叫"兰多姆" 还有一个叫"安娜" (阿纳纳斯是法国的菠萝)

随机是我的主课... 但出于某种原因,我的主课(兰多姆)没有检测到肛门炎

这是我在阿纳纳斯的剧本:

public class ananas {
    public String a(String PackageA){
        PackageA = "This file shall remain TOP SECRET! The ultimate universal secret code is... Ananas "; 
        return PackageA;
    }
    public String b(String PackageB){
        PackageB = "File not created yet";
        return PackageB;
    }
    public String c(String PackageC){
        PackageC = "File not created  yet";
        return PackageC;
    }

}

这是我在《兰多姆》中的代码:

import java.util.Scanner;
public class Random {
    public static void main(String ars[]){
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome, Please enter the code: "); 
        String hey = input.nextLine();
            if(hey .equals("The sandman ate my dollar"))

                System.out.println("Welcome! Please choose one: A), B), C)");
                    Scanner input2 = new Scanner(System.in);
                    String heyy = input2.nextLine();
                    if(heyy .equals("A)"))
                        System.out.println("File  not created yet");

                    else if(heyy .equals("B)"))
                        System.out.println("Flid not created yet");

                    else if(heyy .equals("C)"))
                        System.out.println("File not created yet");
                    else 
                        System.out.println("Access Denied"); 

我想说:"ananas abc = new ananas (;)"

但即使我去运行我的代码, 它也只能探测到"兰多"

帮帮忙吧?

问题回答

If that is all the code you have in Random, you are never constructing an instance of ananas. As your methods in Ananas are not static, you need to create an instance of the class.

Ananas a = new Ananas(); // Construct new instance calling the default constructor

// Note that you have named your methods so that nobody can really understand what they do!
// Now, to call methods from this class, you would do it like this
//First a = the instance of ananas class we just built. The second a is the method in the class we wish to call. String is the parameter the method requires.
a.a(string);

当它看起来你想从类 Ananas 中引用该方法时, 取决于用户给出的输入, 您可以修改您的代码来进行这样的修改 。

if(heyy.equals("A)"){
      a.a(yourString); // You need to create the ananas instance before this, and have a string called yourString that you pass on to the method
}

在此情况下,更好的解决办法是,不要求用肛门中的方法来要求字符串参数。 还要考虑命名方法, 以便描述它正在做什么! 所需的修改将简单到 :

public String a(){ // a could be something like  getStringA 
        String PackageA = "This file shall remain TOP SECRET! The ultimate universal secret code is... Ananas "; 
        return PackageA;
    }

我看不到阿纳纳斯的建筑师

它应该有如下内容:

public ananas(){
System.out.println("I like pineapples");
}

我希望这有帮助:)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签