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

时间:2017-10-12 03:45:03

标签: java netbeans

我正在完成一项任务,但我无法弄明白该怎么做。我有三个不同的Java类。我试图在一个类中使用这些方法在不同的类中执行某些操作。我正在制作一个非常原始的播放列表程序。我必须检查播放列表是否已满,如果不是我必须询问标题和艺术家。然后我必须使用标题和艺术家作为参数来调用我的方法。我想知道是否有人能指出我正确的方向,我必须做什么来调用方法?我仍然不完全理解循环,但我知道我必须使用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");

   }
   }

PlayList Class

public class PlayList {
private Song [] songs;
private int filled;

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

歌曲课程

public class Song {
String title;
String artist; 

public Song (String t, String a) {
title = t;
artist = a; 
}
public String toString() {
return "Title: " + title + " " + "Artist: " + artist;

}

}

2 个答案:

答案 0 :(得分:0)

首先,您使用 isFull PlayList 的功能错误。

for (int i = 0; i <= PlayList.isFull(title, artist);i++)
  1. isFull 是一个无参数函数,您正在使用它传递2个参数。

  2. isFull 函数返回一个布尔值(即true / false),但是您将它与 int 进行比较,这没有任何意义。< / p>

  3. isFull 不是静态功能。因此,您不能直接将其与类名一起使用。

  4. - 您需要将函数 isFull 声明为静态。

    public static boolean isFull()
    

    - 或者您需要在类 Main 中创建类 PlayList 的对象,然后使用该对象调用该函数。

    此外,您的功能删除未执行任何任务

     if (songs[i] == null){
            songs[i] = null;
    }
    

    正在检查 songs [i] 是否已经为空,然后将其设置为null,这没有任何意义。

    你应该增加 i (即i ++)不减少它(即我 - )

    for (int i = 0; i < songs.length; i--)
    

答案 1 :(得分:0)

If you want to call method from another class that method must be a static method. Then you can call it using Class name and Method name. For an example;

public class main(){
    A a = new A();
    a.x();
}
public class A{
    public static void x(){};
}

You called isFull method with two parameters but your PlayList class does not have any parameter for isFull method. That is an error. I re-write your assignment class set using ArrayList for PlayList class. Follow this codes. Hope you can understand it's concept of OOP(Follow this tutorials. https://www.javatpoint.com/java-oops-concepts).

Main Class

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");

    }
}

PlayList Class

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();
    }
}

Song Class is same as you wrote.