尝试在方法 Java 中调用 class 的一部分时出错
Error trying to call parts of a class in a method Java
我离最终完成这项任务如此之近,但我遇到了障碍。出于某种原因,我无法在我的方法中获得 class 的调用方面。有没有人有办法做到这一点?我在我所指的三个部分旁边添加了评论。我不知道是什么导致了这些错误,非常感谢您的帮助。
public class Main
{
public static int w = -1;
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static String[] names;
public static int[] years;
public static String[] studios;
public static class Movie{
// instance variables
private int year;
private String title;
private String studio;
private int placement;
// Constructor for objects of class Movie
Movie(String title, int year, String studio)
{
// initialize instance variables
this.title = title;
this.year = year;
this.studio = studio;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public void setPlacement() {
w+=1;
this.placement = w;
}
public int getPlacement() {
return placement;
}
public String getStudio()
{
return studio;
}
public void setStudio(String studio)
{
this.studio = studio;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String toString()
{
String str = String.format("%-30s %4d %-20s", title, year, studio);
return str;
}
}
public static void arrNames(String name) {
x+=1;
names = new String[x];
names[(x-1)] = name.getTitle(); //error here, why can't I just use getTitle()?
}
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
public static void arrStudio(String name) {
z+=1;
studios = new String[z];
studios[z-1] = name.getStudio(); //same error
}
public static void setGroup(String name) {
arrNames(name);
arrYear(name);
arrStudio(name);
}
public static void getGroups(String name) {
int a = x;
int b = y;
int c = z;
int d = 0;
int e = 0;
int f = 0;
System.out.println("Names Category: ");
while (a > 0) {
System.out.print(names[d] + " ");
d+=1;
a-=1;
}
System.out.println("Years Category: ");
while (b > 0) {
System.out.print(years[e] + " ");
e+=1;
b-=1;
}
System.out.println("Studios Category: ");
while (c > 0) {
System.out.print(studios[f] + " ");
f+=1;
c-=1;
}
public static void main(String[] args) {
Movie nemo = new Movie("Finding Nemo",2003,"Pixar");
setGroup(nemo.getTitle());
}
}
我举这个例子:
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
您正在尝试对变量名称使用方法 getYear,而该名称是一个字符串而不是电影对象
您可以像这样将参数从 String 替换为 Movie:
public static void arrYear(Movie name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
它应该适用于您的所有功能
我离最终完成这项任务如此之近,但我遇到了障碍。出于某种原因,我无法在我的方法中获得 class 的调用方面。有没有人有办法做到这一点?我在我所指的三个部分旁边添加了评论。我不知道是什么导致了这些错误,非常感谢您的帮助。
public class Main
{
public static int w = -1;
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static String[] names;
public static int[] years;
public static String[] studios;
public static class Movie{
// instance variables
private int year;
private String title;
private String studio;
private int placement;
// Constructor for objects of class Movie
Movie(String title, int year, String studio)
{
// initialize instance variables
this.title = title;
this.year = year;
this.studio = studio;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public void setPlacement() {
w+=1;
this.placement = w;
}
public int getPlacement() {
return placement;
}
public String getStudio()
{
return studio;
}
public void setStudio(String studio)
{
this.studio = studio;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String toString()
{
String str = String.format("%-30s %4d %-20s", title, year, studio);
return str;
}
}
public static void arrNames(String name) {
x+=1;
names = new String[x];
names[(x-1)] = name.getTitle(); //error here, why can't I just use getTitle()?
}
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
public static void arrStudio(String name) {
z+=1;
studios = new String[z];
studios[z-1] = name.getStudio(); //same error
}
public static void setGroup(String name) {
arrNames(name);
arrYear(name);
arrStudio(name);
}
public static void getGroups(String name) {
int a = x;
int b = y;
int c = z;
int d = 0;
int e = 0;
int f = 0;
System.out.println("Names Category: ");
while (a > 0) {
System.out.print(names[d] + " ");
d+=1;
a-=1;
}
System.out.println("Years Category: ");
while (b > 0) {
System.out.print(years[e] + " ");
e+=1;
b-=1;
}
System.out.println("Studios Category: ");
while (c > 0) {
System.out.print(studios[f] + " ");
f+=1;
c-=1;
}
public static void main(String[] args) {
Movie nemo = new Movie("Finding Nemo",2003,"Pixar");
setGroup(nemo.getTitle());
}
}
我举这个例子:
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
您正在尝试对变量名称使用方法 getYear,而该名称是一个字符串而不是电影对象 您可以像这样将参数从 String 替换为 Movie:
public static void arrYear(Movie name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
它应该适用于您的所有功能