简体   繁体   English

在主程序中调用方法

[英]Calling methods in the main program

I am trying to access some information that my program isnt letting me. 我试图访问我的程序不允许我的一些信息。 in my method "addTrack" it doesnt recognise the myTracklist object. 在我的方法“addTrack”中,它不识别myTracklist对象。 how do i call myTracklist.count by using a method in the main class? 如何通过使用主类中的方法调用myTracklist.count?

public class CD {
    String art;
    String tit;
    public CD(String artist, String title){
        art = artist;
        tit = title;
        tracklist myTracklist = new tracklist(100);
    }

    public static void main(String[] args) {
        String mainArtist;
        String mainTitle;
        CD myCD = new CD("Awesomeguy", "AwesomeCDName");
        mainArtist = myCD.getArtist();
        System.out.println(mainArtist);
        mainTitle = myCD.getTitle();
        System.out.println(mainTitle);
        myCD.display();
    }

    public String getArtist(){
        String person;
        person = art;
        return person;
    }

    public String getTitle(){
        String name;
        name = tit;
        return name;
    }

    public boolean addTrack(String trackinfo){
        boolean result = false;
        if (myTracklist.count < 100){
            myTracklist.add(trackinfo);
            result = true;
        }
        return result;
    }

    public int numTracks(){
        int amount;
        amount = myTracklist.count();
        return amount;
    }

    public void display(){
        System.out.println("Artist = "+ art);
        System.out.println("Title = "+ tit);
        tracklist.display();
    }
}

here is my tracklist class 这是我的跟踪列表

public class tracklist {
    int length;
    int numUsed;
    String[] storage;

    public tracklist(int size){
    length = size;
    numUsed = 0;
    storage = new String[length];
    }

    public int count(){
        return numUsed;
    }
}

You've got scope problems in that you're declaring tracklist inside the CD constructor so that it only exists inside of the constructor and nowhere else. 您有范围问题,因为您在CD构造函数中声明了tracklist,因此它只存在于构造函数内部而不是其他地方。 You must make it a field that is declared in the class for it to be usable at all the methods of the class. 您必须使它成为类中声明的字段,以便它可以在类的所有方法中使用。

So instead of 而不是

public class CD {
    String art;
    String tit;
    public CD(String artist, String title){
        art = artist;
        tit = title;
        tracklist myTracklist = new tracklist(100);
    }

do

public class CD {
    private String art;
    private String tit;
    private tracklist myTracklist; // declared

    public CD(String artist, String title){
        art = artist;
        tit = title;
        myTracklist = new tracklist(100); // initialized
    }

    // getter and setter methods of course.

It's a subtle but important distinction. 这是一个微妙但重要的区别。

As an aside: you'll want to learn Java naming conventions so that others can more readily understand your code and your questions. 暂且不说:您需要学习Java命名约定,以便其他人可以更容易地理解您的代码和问题。 Class names begin with an upper case letter. 类名以大写字母开头。

As a second aside: don't have outside classes directly manipulate class fields. 作为第二个旁边:没有外部类直接操纵类字段。 Use private fields and public getters and setters to allow the class to have more control over just what can be seen and what can be done. 使用私有字段和公共getter和setter可以让类更好地控制可以看到的内容和可以执行的操作。

Keep the declaration of myTrackList outside the constructor . myTrackList的声明保留在构造函数之外。 Like this:- 像这样:-

String tit;
tracklist myTracklist;

public CD(String artist, String title){
    art = artist;
    tit = title;
    myTracklist = new tracklist(100);
}

it should be myTracklist.count() here: 它应该是myTracklist.count()

if (myTracklist.count < 100){

Also: 也:

String tit; // oh I love this object name!
tracklist myTracklist;
public CD(String artist, String title){
    art = artist;
    tit = title;
    myTracklist = new tracklist(100);
}

Since you have declared it in your customer the "myTracklist" variable will only be visible in your constructor. 由于您已在客户中声明了它,因此“myTracklist”变量仅在构造函数中可见。 Hence "addTrack" doesnt recognise the myTracklist object. 因此“addTrack”无法识别myTracklist对象。

Declare it globally, 全局声明它,

public class CD {
    String art;
    String tit;
    tracklist myTracklist;

And initialize in the constructor as below. 并在构造函数中初始化如下。

myTracklist = new tracklist(100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM