package fanxing;
//书类
public class scbook {
private double price;
private String name;
public scbook(double price, String name) {
super();
this.price = price;
this.name = name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "价格:" + price + "书名:" + name + "]";
}
public String getName() {
return name;
}
}
package fanxing;
//电脑类
public class ScCumputer {
private double price;
private String name;
public ScCumputer(double price, String name) {
super();
this.price = price;
this.name = name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "价格:" + price + "电脑名:" + name + "]";
}
public String getName() {
return name;
}
}
package fanxing;
//仓库类 使用对象数组
public class Warehouse {
private scbook[] bookWarehouse;
private ScCumputer[] cumputerWarehouse;
//存书
public void setBook(scbook[]book){//这里使用set方法
this.bookWarehouse =book;
}
//查看书
public void chakanBOOK(){
for(scbook b:bookWarehouse){//默认调用toString方法
System.out.println(b);
}
}
public void setcumputer(ScCumputer[] cumputer){
this.cumputerWarehouse=cumputer;//存入电脑的数据
}
public void ScCumputer(){
for(ScCumputer c:cumputerWarehouse){//foreach循环
System.out.println(c);
}
}
}
package fanxing;
//测试类
public class Test {
public static void main(String[] args) {//主方法
scbook[]book =new scbook[]{
new scbook(27.5,"水好深"),
new scbook(28.5,"水a好深"),
new scbook(35.5,"水v好深"),
new scbook(42.5,"水好w深"),
};
ScCumputer[] cumputer =new ScCumputer[]{
new ScCumputer(4562.0,"微软"),
new ScCumputer(42342.0,"外星人"),
new ScCumputer(4532.0,"联想"),
new ScCumputer(5321.0,"小米"),
};
Warehouse store =new Warehouse();//调用仓库的类new一个空间
store.setBook(book);
store.chakanBOOK();
store.setcumputer(cumputer);
store.ScCumputer();
}
}
打印查看的结果
价格:27.5书名:水好深]
价格:28.5书名:水a好深]
价格:35.5书名:水v好深]
价格:42.5书名:水好w深]
价格:4562.0电脑名:微软]
价格:42342.0电脑名:外星人]
价格:4532.0电脑名:联想]
价格:5321.0电脑名:小米]
//仓库泛型使用格式 //C可改变但是一定要与下面保持一致。 //相比上面方式简单方便更实用。
public class Warehouse<C> {
private C[] warehouse;
//存书
public void deposit(C[]e){//这里使用set方法
this.warehouse =e;
}
//查看书
public void viev(){
for(C e:warehouse){//默认调用toString方法
System.out.println(e);
}
}
}
//测试类泛型使用格式
public class Test {
public static void main(String[] args) {
scbook[]book =new scbook[]{
new scbook(27.5,"水好深"),
new scbook(28.5,"水a好深"),
new scbook(35.5,"水v好深"),
new scbook(42.5,"水好w深"),
};
ScCumputer[] cumputer =new ScCumputer[]{
new ScCumputer(4562.0,"微软"),
new ScCumputer(42342.0,"外星人"),
new ScCumputer(4532.0,"联想"),
new ScCumputer(5321.0,"小米"),
};
Warehouse<scbook> store =new Warehouse<scbook>();//方法所在的类<内为你想要引用属性的类>后面接你的对象在创造NEW空间; <>泛型可以为任意类型,直到定义才确定类型,泛型方法独立于类也可以变化。
store.deposit(book);
store.viev();
Warehouse<ScCumputer> storeto =new Warehouse<ScCumputer>();
storeto.deposit(cumputer);
store.viev();
}
}
泛型方法
package fanxing;
import fanxing.scbook;
public class ToTest {
public static void main(String[] args) {
print("这是字符串");
print(789);
print(new scbook(78.5,"《飘》"));//必须和所引用的类的构造方法数据类型相符
}
//泛型方法要定义在返回值之前
public static<E>void print (E e){
System.out.println(e);
}
}
打印结果:
这是字符串
789
价格:78.5书名:《飘》]
小生根据学习所写如有改进还请各位仁兄多多指点!