我正在使用Eclipse IDE。 我正在创建一个程序来制作对象数组并使用扫描仪类从用户那里获取输入。 当我尝试将输入设置为方法变量时,出现错误: 未为类型BookDetails 定义方法set_no(Int),其中, set_no是Book类的方法,而 BookDetails是主类。而所有4种方法都可以得到它。
代码如下:
这是Book类:
import {authProvider} from "../../app/AuthProvider";
import {render} from "@testing-library/react";
jest.mock('../../app/AuthProvider');
describe('Component', () => {
it('should match snapshot', () => {
authProvider.getAccessToken.mockResolvedValue({
accessToken: 'accessToken'
});
const {container} = render(Component);
expect(container.firstChild).toMatchSnapshot("Component");
});
});
这是主要课程:
import java.util.Scanner;
class Book {
private int bookno;
private String title;
private String author;
private float price;
public Book(){
bookno=0;
title="Anonymous";
author="Anonymous";
price=0;
}
public void set_no(int no) {
this.bookno=no;
}
public void set_title(String t) {
if(t.length()<4) {
System.out.println("Error, the title of the book can't be less than 4 characters\n");
}
else {
this.title=t;
}
}
public void set_author(String a) {
this.author=a;
}
public void set_price(int p) {
if(p<1 || p>5000) {
System.out.println("Error, the price is out of range\n");
}
else {
this.price=p;
}
}
@Override
public String toString() {
String s = "Book number: "+Integer.toString(bookno)+",\n"+"Title: "+title+",\n"+"Author: "+author+",\n"+"Price: Rs. "+Float.toString(price)+".\n";
return s;
}
}
答案 0 :(得分:0)
set_no
是Book
的方法,因此您需要在Book
的实例(在本例中为b[i]
)上调用它。
因此,编写b[i].set_no(sc.nextInt())
(对于其他方法也是如此)。
答案 1 :(得分:0)
Java中的字母和二传手与TypeScript / JavaScript不同,您不需要Java(_
)中的下划线
我将您的代码更改为Java函数之类的getter和setter:
import java.util.Scanner;
class Book {
private int bookNo;
private String title;
private String author;
private float price;
public Book(){
bookNo=0;
title="Anonymous";
author="Anonymous";
price=0;
}
public void setBookNo(int newBookNo) {
this.bookno=newBookNo;
}
public void setTitle(String newTitle) {
if(newTitle.length()<4) {
System.out.println("Error, the title of the book can't be less than 4 characters\n");
}
else {
this.title=newTitle;
}
}
public void setAuthor(String newAuthor) {
this.author=newAuthor;
}
public void setPrice(int newPrice) {
if(newPrice<1 || newPrice>5000) {
System.out.println("Error, the price is out of range\n");
}
else {
this.price=newPrice;
}
}
@Override
public String toString() {
String s = "Book number: "+Integer.toString(bookNo)+",\n"+"Title: "+title+",\n"+"Author: "+author+",\n"+"Price: Rs. "+Float.toString(price)+".\n";
return s;
}
}