做菜鸟真的很痛苦!这么简单的问题都搞不懂。怎么样才能继承类啊。
大3,刚学java不久,下面的程序貌似很长,拜托各位大侠ctrl+c一下帮我看一下。我想单独建一个书库类,为什么构造不起来哩?也许是我的概念没弄懂吧。
谢谢,祝各位大侠新年好运~~~
import java.io.*;
import java.util.*;
public class stock
{
String name;
String author;
int price;
stock(String a,String b,int c)
{
name=a;
author=b;
price=c;
}
void print()
{
System.out.println( "name is "+name);
System.out.println( "author is "+author);
System.out.println( "price is "+price);
}
void get()
{
int N=100;
stock []a=new stock[N];
a[0]=new stock( "Java ", "PlayGrrrr... ",12);
a[1]=new stock( "C# ", "Bill Gates ",13);
a[2]=new stock( "Delphi ", "Boxer ",14);
a[3]=new stock( "Oracle ", "zy ",15);
}
}
class process
{
void step1()
{
System.out.println( "do what? ");
System.out.println( "a.search b.add c.exit ");
char ch=0;
try
{
ch=(char)System.in.read();
System.in.skip(2);
}catch(IOException z){}
if(ch== 'a ')
{
System.out.println( "a.by name b.by author ");
char ch2=0;
try
{
ch2=(char)System.in.read();
System.in.skip(2);
}catch(IOException z){}
if(ch2== 'a ')
{
System.out.print( "plz input the book 's name: ");
InputDate fuck=new InputDate();
fuck.findbookname();
}
if(ch2== 'b ')
{
System.out.print( "plz input the author 's name: ");
InputDate fuck2=new InputDate();
fuck2.findauthorname();
}
}
if(ch== 'b ')
{
InputDate fuck3=new InputDate();
fuck3.add();
}
if(ch== 'c ')
{
System.out.println( "c u ");
}
}
}
class InputDate
{
static private String s;
static public void input()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
s=br.readLine();
}catch(IOException e){}
}
static public String getString()
{
input();
return s;
}
static public int getInt()
{
input();
return Integer.parseInt(s);
}
public void findbookname()
{
/*int N=100;
stock []a=new stock[N];
a[0]=new stock( "Java ", "PlayGrrrr... ",12);
a[1]=new stock( "C# ", "Bill Gates ",13);
a[2]=new stock( "Delphi ", "Boxer ",14);
a[3]=new stock( "Oracle ", "zy ",15);*/
stock mis=new stock();
mis.get();
s=InputDate.getString();
Vector names=new Vector();
for(int i=0;i <a.length&&a[i+1]!=null;i++)
{
names.add(a[i].name);
}
if(names.indexOf(s)!=-1)
{
System.out.println( "u can fint it here! ");
}
else
System.out.println( "sorry,there is not such a book! ");
}
public void findauthorname()
{
s=InputDate.getString();
/*int N=100;
stock []a=new stock[N];
a[0]=new stock( "Java ", "PlayGrrrr... ",12);
a[1]=new stock( "C# ", "Bill Gates ",13);
a[2]=new stock( "Delphi ", "Boxer ",14);
a[3]=new stock( "Oracle ", "zy ",15);*/
Vector authornames=new Vector();
for(int i=0;i <a.length&&a[i+1]!=null;i++)
{authornames.add(a[i].author);}
if(authornames.indexOf(s)!=-1)
{
System.out.println( "there is such a book! ");}
else
System.out.println( "i didn 't hear that pl at all! ");
}
public void add()
{
/*int N=100;
stock []a=new stock[N];
a[0]=new stock( "Java ", "PlayGrrrr... ",12);
a[1]=new stock( "C# ", "Bill Gates ",13);
a[2]=new stock( "Delphi ", "Boxer ",14);
a[3]=new stock( "Oracle ", "zy ",15);*/
System.out.println( "plz input info: ");
String ab,b;
int c;
ab=InputDate.getString();
b=InputDate.getString();
c=InputDate.getInt();
int i=0;
while(a[i]!=null)i++;
a[i]=new stock(ab,b,c);
System.out.println( "successfully added ");
process a1=new process();
a1.step1();
}
}
class main
{
public static void main(String args[])
{
process a1=new process();
a1.step1();
}
}
[解决办法]
我汗啊,几天没上csdn,就让人说了一顿。你新加的书查不到,是你的逻辑问题啊。你很多地方都是重新实例化然后在操作,你所引用的对象都不是同一个啊.......有多处都是这样哦!
我按照你的需求在你原来上面改动了一下。我把代码给你,自己看看吧
import java.io.*;
import java.util.*;
class stock {
private String name;
private String author;
private int price;
// 所有书籍
private ArrayList books = new ArrayList() ;
public stock() {
// 初始化时添加书
this.books.add(new stock( "Java ", "PlayGrrrr... ", 12));
this.books.add(new stock( "C# ", "Bill Gates ", 13));
this.books.add(new stock( "Delphi ", "Boxer ", 14));
this.books.add(new stock( "Oracle ", "zy ", 15));
}
public stock(String a, String b, int c) {
name = a;
author = b;
price = c;
}
/* 为所有属性公开getter和setter */
public void print() {
System.out.println( "name is " + name);
System.out.println( "author is " + author);
System.out.println( "price is " + price);
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public ArrayList getBooks() {
return books;
}
public void setBooks(ArrayList books) {
this.books = books;
}
}
class process {
private InputDate fuck = new InputDate(); // 操作同一个对象
void step1() {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String res = " ";
char c[]={};
try {
do
{
System.out.println( "do what? ");
System.out.println( "1.search 2.add 3.exit ");
int flag = Integer.parseInt(read.readLine());
switch(flag)
{
case 1:
System.out.println( "a.by name b.by author ");
char ch2 = 0;
try {
ch2 = (char) System.in.read();
System.in.skip(2);
} catch (IOException z) {
}
if (ch2 == 'a ') {
System.out.print( "plz input the book 's name: ");
fuck.findbookname();
}
if (ch2 == 'b ') {
System.out.print( "plz input the author 's name: ");
fuck.findauthorname();
}
break;
case 2:
fuck.add();
break;
case 3:
System.out.println( "c u ");
break;
default:
System.out.println( "your input has false! ");
break;
}
System.out.println( "again?y/n ");
res = read.readLine();
c = res.toCharArray();
}
while(c[0] == 'Y ' || c[0] == 'y ');
System.out.println( "Welcome Next! ");
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class InputDate {
private stock mis = new stock(); // 操作同一对象
static private String s;
static public void input() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
s = br.readLine();
} catch (IOException e) {
}
}
static public String getString() {
input();
return s;
}
static public int getInt() {
input();
return Integer.parseInt(s);
}
/**
* 按名称查找书籍
* 检索stock类的所有书籍books
*/
public void findbookname() {
s = InputDate.getString();
ArrayList books = mis.getBooks();
for(Iterator iter = books.iterator();iter.hasNext();)
{
if(((stock)iter.next()).getName().equals(s))
{
System.out.println( "u can fint it here! ");
return;
}
}
System.out.println( "sorry,there is not such a book! ");
}
// 按作者查找书籍
public void findauthorname() {
s = InputDate.getString();
ArrayList books = mis.getBooks();
for(Iterator iter = books.iterator();iter.hasNext();)
{
if(((stock)iter.next()).getAuthor().equals(s))
{
System.out.println( "u can fint it here! ");
return;
}
}
System.out.println( "sorry,there is not such a book! ");
}
// 添加书籍
public void add() {
System.out.println( "plz input info: ");
String ab, b;
int c;
ab = InputDate.getString();
b = InputDate.getString();
c = InputDate.getInt();
ArrayList books = mis.getBooks();
books.add(new stock(ab,b,c));
System.out.println( "successfully added ");
}
}
public class main {
public static void main(String args[]) {
process a1 = new process();
a1.step1();
}
}
[解决办法]
接近右上角 有个“管理”