Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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_Netbeans - Fatal编程技术网

在Java中,如何使用主类中的子类中的方法?

在Java中,如何使用主类中的子类中的方法?,java,netbeans,Java,Netbeans,我正在做一项作业,不知道该做什么。我有三个不同的Java类。我试着用一个类中的方法在另一个类中做一些事情。我正在制作一个非常原始的播放列表程序。我必须检查播放列表是否已满,如果没有,我必须询问标题和艺术家。然后我必须使用title和artist作为参数调用我的方法。我想知道是否有人能为我指明正确的方向,告诉我如何调用该方法?我仍然不完全理解循环,但我知道我必须使用for循环才能做到这一点。谢谢你抽出时间 这是我的密码: 主类 import java.util.Scanner; public cl

我正在做一项作业,不知道该做什么。我有三个不同的Java类。我试着用一个类中的方法在另一个类中做一些事情。我正在制作一个非常原始的播放列表程序。我必须检查播放列表是否已满,如果没有,我必须询问标题和艺术家。然后我必须使用title和artist作为参数调用我的方法。我想知道是否有人能为我指明正确的方向,告诉我如何调用该方法?我仍然不完全理解循环,但我知道我必须使用for循环才能做到这一点。谢谢你抽出时间

这是我的密码:

主类

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
   PlayList p = new PlayList (5);
   Scanner sc = new Scanner(System.in);
   String command;
   String title;
   String artist;



  System.out.println("Enter a to add, r to remove, d to display,or q to 
  quit:");
  command = sc.nextLine();
  while (!command.equals("q")) {
  // Interpret command
  if (command.equals("a")) {
   //add song
 for (int i = 0; i <= PlayList.isFull(title, artist);i++) {
     if(songs[i])== null {
     songs[i] = filled;
  }


  }

  } else if (command.equals("r")) {
  // Remove a song
  System.out.print("Title: ");
  title = sc.nextLine();
  p.remove(title);
  } else if (command.equals("d")) {
  // Fill this in
  }
  // Get the next command
  System.out.println("Enter a to add, r to remove, d to display, or q to 
  quit:");
  command = sc.nextLine();
   }
  System.out.println("Program Ended");

   }
   }
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        PlayList p = new PlayList (5);
        Scanner sc = new Scanner(System.in);
        String command;
        String title;
        String artist;


        System.out.println("Enter a to add, r to remove, d to display,or q to quit:");
        command = sc.nextLine();
        while (!command.equals("q")) {
            // Interpret command
            if (command.equals("a")) {
                //add song
                System.out.println("Enter Title:");
                title = sc.nextLine();
                System.out.println("Enter Artist:");
                artist = sc.nextLine();
                if(!p.isFull()) {
                    p.add(title, artist);
                    System.out.println("Added Success!");
                }
                else
                    System.out.println("Sorry,Playlist is full");

            } else if (command.equals("r")) {
                // Remove a song
                System.out.print("Title: ");
                title = sc.nextLine();
                p.remove(title);
            } else if (command.equals("d")) {
                // Fill this in
                p.display();
            }
            // Get the next command
            System.out.println("Enter a to add, r to remove, d to display, or q to quit:");
            command = sc.nextLine();
        }
        System.out.println("Program Ended");

    }
}

首先,您使用的isFull播放列表的功能是错误的

for (int i = 0; i <= PlayList.isFull(title, artist);i++)
-或者,您需要在classMain中创建classPlayList对象,然后使用该对象调用

此外,您的函数删除不执行任何任务

 if (songs[i] == null){
        songs[i] = null;
}
它正在检查歌曲[i]
是否已经为null,然后将其设置回null,这没有任何意义

您应该递增i(即i++),而不是递减它(即i--)

for(int i=0;i
如果要从另一个类调用方法,该方法必须是
静态方法。然后可以使用
类名
方法名
调用它。 例如,

public class main(){
    A a = new A();
    a.x();
}
public class A{
    public static void x(){};
}
您使用两个参数调用了
isFull
方法,但您的
PlayList
类没有
isFull
方法的任何参数。这是一个
错误
。 我使用
ArrayList
PlayList
类重新编写作业类集。请遵循以下代码。希望您能理解它的OOP概念(遵循本教程)

主类

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
   PlayList p = new PlayList (5);
   Scanner sc = new Scanner(System.in);
   String command;
   String title;
   String artist;



  System.out.println("Enter a to add, r to remove, d to display,or q to 
  quit:");
  command = sc.nextLine();
  while (!command.equals("q")) {
  // Interpret command
  if (command.equals("a")) {
   //add song
 for (int i = 0; i <= PlayList.isFull(title, artist);i++) {
     if(songs[i])== null {
     songs[i] = filled;
  }


  }

  } else if (command.equals("r")) {
  // Remove a song
  System.out.print("Title: ");
  title = sc.nextLine();
  p.remove(title);
  } else if (command.equals("d")) {
  // Fill this in
  }
  // Get the next command
  System.out.println("Enter a to add, r to remove, d to display, or q to 
  quit:");
  command = sc.nextLine();
   }
  System.out.println("Program Ended");

   }
   }
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        PlayList p = new PlayList (5);
        Scanner sc = new Scanner(System.in);
        String command;
        String title;
        String artist;


        System.out.println("Enter a to add, r to remove, d to display,or q to quit:");
        command = sc.nextLine();
        while (!command.equals("q")) {
            // Interpret command
            if (command.equals("a")) {
                //add song
                System.out.println("Enter Title:");
                title = sc.nextLine();
                System.out.println("Enter Artist:");
                artist = sc.nextLine();
                if(!p.isFull()) {
                    p.add(title, artist);
                    System.out.println("Added Success!");
                }
                else
                    System.out.println("Sorry,Playlist is full");

            } else if (command.equals("r")) {
                // Remove a song
                System.out.print("Title: ");
                title = sc.nextLine();
                p.remove(title);
            } else if (command.equals("d")) {
                // Fill this in
                p.display();
            }
            // Get the next command
            System.out.println("Enter a to add, r to remove, d to display, or q to quit:");
            command = sc.nextLine();
        }
        System.out.println("Program Ended");

    }
}
播放列表

import java.util.ArrayList;
import java.util.List;

public class PlayList {
    private static List<Song> songs;
    private static int filled;
    private  static int size = 0;

    public PlayList (int s){
        songs = new ArrayList<>();
        size = s;
    }
    public static boolean isFull() {
        return (filled == size);
    }
    public static void add(String t, String a) {
        songs.add(new Song(t,a));
                filled++;
    }
    public void display() {
        for (int i = 0; i < songs.size(); i++){
            if (songs.get(i) != null) {
                System.out.println(songs.get(i));
            }
        }
    }
    public void remove(String t) {
        //return t?
        for (int i = 0; i < songs.size(); i++){
            if (songs.get(i).title == t){
                songs.remove(i);
                break;
            }
        }
    }
    public static int getSize(){
        return songs.size();
    }
}
import java.util.ArrayList;
导入java.util.List;
公共类播放列表{
私有静态列表歌曲;
私有静态int填充;
私有静态int size=0;
公共播放列表(int-s){
歌曲=新的ArrayList();
尺寸=s;
}
公共静态布尔值isFull(){
返回(填充==大小);
}
公共静态无效添加(字符串t、字符串a){
歌曲。添加(新歌(t,a));
填充++;
}
公共空间显示(){
对于(int i=0;i

Song
类与您编写的相同。

查看您编写
p.remove(标题)的
main
中的那一行?这一行的出现向我表明,您已经知道如何在另一个类的代码中从一个类调用方法。这将导致编译错误。您应该检查错误并通过google了解它创建该子类的对象并使用点表示法调用函数。了解如何将静态内容访问到非静态内容,反之亦然。
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        PlayList p = new PlayList (5);
        Scanner sc = new Scanner(System.in);
        String command;
        String title;
        String artist;


        System.out.println("Enter a to add, r to remove, d to display,or q to quit:");
        command = sc.nextLine();
        while (!command.equals("q")) {
            // Interpret command
            if (command.equals("a")) {
                //add song
                System.out.println("Enter Title:");
                title = sc.nextLine();
                System.out.println("Enter Artist:");
                artist = sc.nextLine();
                if(!p.isFull()) {
                    p.add(title, artist);
                    System.out.println("Added Success!");
                }
                else
                    System.out.println("Sorry,Playlist is full");

            } else if (command.equals("r")) {
                // Remove a song
                System.out.print("Title: ");
                title = sc.nextLine();
                p.remove(title);
            } else if (command.equals("d")) {
                // Fill this in
                p.display();
            }
            // Get the next command
            System.out.println("Enter a to add, r to remove, d to display, or q to quit:");
            command = sc.nextLine();
        }
        System.out.println("Program Ended");

    }
}
import java.util.ArrayList;
import java.util.List;

public class PlayList {
    private static List<Song> songs;
    private static int filled;
    private  static int size = 0;

    public PlayList (int s){
        songs = new ArrayList<>();
        size = s;
    }
    public static boolean isFull() {
        return (filled == size);
    }
    public static void add(String t, String a) {
        songs.add(new Song(t,a));
                filled++;
    }
    public void display() {
        for (int i = 0; i < songs.size(); i++){
            if (songs.get(i) != null) {
                System.out.println(songs.get(i));
            }
        }
    }
    public void remove(String t) {
        //return t?
        for (int i = 0; i < songs.size(); i++){
            if (songs.get(i).title == t){
                songs.remove(i);
                break;
            }
        }
    }
    public static int getSize(){
        return songs.size();
    }
}