package test1;
import java.util.*;
public class Main{
public static void main(String[] args) {
int k;
//把可能出错的代码放在try当中
try {
//创建对象
WuMingFen w1 = new WuMingFen("牛肉",1,true);
WuMingFen w2 = new WuMingFen("牛肉");
WuMingFen w3 = new WuMingFen();
WuMingFen1 w4 = new WuMingFen1("牛肉",4,true,true);
WuMingFen1 w5 = new WuMingFen1("牛肉",5,true,true);
//使用封装的方法为私有属性赋值
w3.setTheMa("牛肉");
w3.setQuantity(3);
w3.setLikeSuop(false);
//使用对象中的方法
w1.check();
k=w1.bowl2(1);
System.out.println("顾客要买"+k+"碗");
w2.check();
k=w2.bowl2(2);
System.out.println("顾客要买"+k+"碗");
w3.check();
k=w3.bowl2(3);
System.out.println("顾客要买"+k+"碗");
w4.check();
k=w4.bowl2(4);
System.out.println("顾客要买"+k+"碗");
//查看顾客总数
System.out.println("今天共有"+Customer.sum+"位顾客");
System.out.println("------------------------");
//创建集合
ArrayList list = new ArrayList();
list.add(w1);
list.add(w2);
list.add(w3);
list.add(w4);
//输出集合当中的数据
System.out.println(list);
System.out.println("++++++++++++++");
//遍历集合
Bianli bianli = new Bianli();
bianli.bianli(list);
}
catch (Exception e){
System.out.println(e);
}
}
}
//静态方法记录今天的顾客总数
class Customer{
static int sum=0;
}
//定义一个接口
interface bowl{
//在接口中定义的方法不能有方法体,只能让实现接口的类重写方法体
public int bowl2(int k);
}
//创建一个无名粉的类来实现接口,不重写接口中的方法体会报错
class WuMingFen implements bowl{
//私有属性
private String theMa="牛肉";
private int quantity=2;
private Boolean likeSuop=true;
//构造器,在创建对象时设置属性,不同的形式参数可以实现重载
public WuMingFen(){
Customer.sum++;
}
public WuMingFen(String theMa){
Customer.sum++;
this.theMa=theMa;
}
public WuMingFen(String theMa,int quantity,Boolean likeSuop){
Customer.sum++;
this.theMa=theMa;
this.likeSuop=likeSuop;
this.quantity=quantity;
}
//封装私有属性
public void setTheMa(String theMa) {
this.theMa = theMa;
}
public String getTheMa(){
return theMa;
}
public void setLikeSuop(Boolean likeSuop) {
this.likeSuop = likeSuop;
}
public Boolean getLikeSuop() {
return likeSuop;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
public void check(){
System.out.println("面码"+theMa+"分量"+quantity+"是否带汤"+likeSuop);
}
//重写接口中的方法
public int bowl2(int k){
return k;
}
//如果不重写toString方法则集合输出的是地址值
public String toString(){
return "面码"+theMa+"分量"+quantity+"是否带汤"+likeSuop;
}
}
//创建一个无名粉的子类,子类继承父类后可以使用父类的方法和父类实现的接口
class WuMingFen1 extends WuMingFen{
private Boolean likeHot;
public WuMingFen1(){
//调用super表示调用父类的构造方法
super();
}
public WuMingFen1(String theMa,int quantity,Boolean likeSuop,Boolean likeHot){
super(theMa,quantity,likeSuop);
this.likeHot=likeHot;
}
//重写父类的check方法
public void check(){
//私有属性只能在本类中使用,想要获取私有属性的值要用get方法
System.out.println("面码"+super.getTheMa()+"分量"+super.getQuantity()+"是否带汤"+super.getLikeSuop()+"是否喜欢辣"+likeHot);
}
public String toString(){
return "面码"+super.getTheMa()+"分量"+super.getQuantity()+"是否带汤"+super.getLikeSuop()+"是否喜欢辣"+likeHot;
}
}
//创建一个遍历集合的类
class Bianli{
public void bianli(List list){
//使用for循环遍历集合
for(int i = 0; i<list.size(); i++){
System.out.println(list.get(i));
}
System.out.println("****************");
//使用增强for遍历集合
for(Object obj:list){
System.out.println(obj);
}
System.out.println("****************");
//使用迭代器遍历集合
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Object obj;
obj=iterator.next();
System.out.println(obj);
}
System.out.println("*************");
//使用lambda表达式遍历集合
list.forEach(i-> System.out.println(i));
System.out.println("********************");
//使用迭代器正序逆序遍历集合
ListIterator iterator1 = list.listIterator();
while (iterator1.hasNext()){
Object obj;
obj=iterator1.next();
System.out.println(obj);
}
while (iterator1.hasPrevious()){
Object obj;
obj=iterator1.previous();
System.out.println(obj);
}
}
}
Output Results: The
surface Code beef component 1 is soup true
customer to buy a bowl
surface Code beef component 2 if soup true
customer to buy two bowls
plane symbols beef component 3 with or without soup false
customer to buy three bowls
plane symbols beef component 4 with or without soup true do you like spicy? true
customers want to buy 4 bowls,
there are 5 customers today
[Whether noodle beef component 1 with soup true, noodle beef component 2 with soup true, noodle beef component 3 with soup false, noodle beef component 4 with soup true whether you like spicy true]
+++++ +++++++++
Noodle code beef component 1 whether with soup true
noodle code beef component 2 whether with soup true
noodle code beef component 3 whether with soup false
noodle code beef component 4 whether with soup true whether you like spicy true
Noodle code beef component 1 whether with soup true
noodle code beef component 2 whether with soup true
noodle code beef component 3 whether with soup false
noodle code beef component 4 whether with soup true whether you like spicy true
Noodle code beef component 1 whether with soup true
noodle code beef component 2 whether with soup true
noodle code beef component 3 whether with soup false
noodle code beef component 4 whether with soup true whether you like spicy true
Noodle code beef component 1 whether with soup true
noodle code beef component 2 whether with soup true
noodle code beef component 3 whether with soup false
noodle code beef component 4 whether with soup true whether you like spicy true
Noodle code beef component 1 whether with soup true
noodle code beef component 2 whether with soup true
noodle code beef component 3 whether with soup false
noodle code beef component 4 whether with soup true whether you like spicy true
noodle code beef component 4 whether with soup true Like the spicy true
noodle code beef portion 3 is it with soup false
noodle code beef portion 2 is it with soup true
noodle code beef portion 1 is it with soup true
Process finished with exit code 0