java小白自学笔记——Day8

                           写在前面——本篇主要是对前几篇的一个例题补充

例1:创建一个五颜六色的接口Colorful,接口有一个表示点亮的方法shine(),规则依据红绿灯规则

package TestProject;
public class TrafficLightClass{
	public static void main(String[] args) {
		RedLightClass redlight=new RedLightClass();//红灯类实例化
		YellowLightClass yellowlight=new YellowLightClass();//黄灯类实例
		GreenLightClass greeenlight=new GreenLightClass();//绿灯类实例
		redlight.shine();
		yellowlight.shine();
		greeenlight.shine();
  }
}
//定义接口
interface Colorful{
	abstract void shine();
}
//定义红灯类
class RedLightClass implements Colorful{
	public void shine()
	{
		System.out.println("亮红灯");
	}
}
//定义黄灯类
class YellowLightClass implements Colorful{
	public void shine()
	{
		System.out.println("亮黄灯");
	}
}
//定义绿灯类
class GreenLightClass implements Colorful{
	public void shine()
	{
		System.out.println("亮绿灯");
	}
}

例2:创建抽象类工厂,中间有一个抽象的生产方法。让汽车厂和鞋厂继承工厂类

package project;
public class FcatoryClass{
	public static void main(String[] args) {
		ShoesFactoryClass shoesfactory=new ShoesFactoryClass();//鞋厂实例化
		CarFactoryClass  carfactory=new CarFactoryClass();//汽车厂实例化
		shoesfactory.show();
		carfactory.show();
  }
}
//定义抽象类——工厂
abstract class FactoryClass{
	abstract void show();
}
//定义鞋厂类
class ShoesFactoryClass extends FactoryClass{
	public void show()
	{
		System.out.println("这是鞋厂,生产鞋子");
	}
}
//定义汽车厂类
class CarFactoryClass extends FactoryClass{
	public void show()
	{
		System.out.println("这是汽车厂,生产汽车");
	}
}

例3:创建猫类,按一定规则输出

package project;
public class CatClass{
	String name;
	int age;
	double weight;
	String color;

	public CatClass(String name,int age,double weight,String color)
	{
		this.name=name;
		this.age=age;
		this.weight=weight;
		this.color=color;
	}
	public static void show(pro p1)//显示方法
	{
		System.out.println("猫咪的名字是:"+p1.name);
		System.out.println("猫咪的年龄是:"+p1.age);
		System.out.println("猫咪的重量是:"+p1.weight);
		System.out.println("猫咪的颜色是:"+p1.color);
	}
	public static void main(String[] args) {
		CatClass cat1=new CatClass("java",12,24.0,"白色");
		CatClass cat2=new CatClass("c",13,21.2,"黑色");
		CatClass cat3=new CatClass("python",11,21.3,"红色");
		show(cat1);
		System.out.println("*********************");
		show(cat2);
		System.out.println("*********************");
		show(cat3);
  }
}

例4:创建usb和typec接口,并用汽车类验证

package packge;
public class TestClss{
	public static void main(String[] args) {
		Car c1=new Car();
		c1.charge();
  }
}
//usb接口
interface USBRecharge{
	abstract void charge();
}
//typec接口
interface TypeCRecharge{
	abstract void charge();
}
//汽车类
class Car implements USBRecharge,TypeCRecharge{
	public void charge(){
		System.out.println("usb和typec都可以充电");
	}
}

例5:模拟餐馆点菜,医生点了鱼香肉丝,学生点了土豆丝

package package;
public class TestClass{
	public static void main(String[] args) {
		coustomer c1=new coustomer("医生");
		coustomer c2=new coustomer("学生");
		System.out.println(c1.name+"点了鱼香肉丝");
		System.out.println(c2.name+"点了土豆丝");
		  }
		}

//定义顾客类
 class coustomer{
	String name;
	public coustomer(String name)
	{
		this.name=name;
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值