zoukankan      html  css  js  c++  java
  • 面向对象案例

    多类合作【重点】

    汽车类和修理厂类分析
    汽车
    	可以看做是一个类
    	成员变量:
    		名字
    		颜色
    		轮胎个数
    		
    	成员方法:
    		飙车
    			飙车有要求,轮胎个数必须是4个
    修理厂
    	可以看做是一个类
    	成员变量:
    		名字
    		地址
    		联系方式
    	成员方法:
    		修理汽车的方法【重点】
    			该方法是需要一个汽车类的对象
    
    5.2 汽车类代码实现
    package com.qfedu.d;
    
    /*
     汽车
    	可以看做是一个类
    	成员变量:
    		名字
    		颜色
    		轮胎个数
    		
    	成员方法:
    		飙车
    			飙车有要求,轮胎个数必须是4个
     */
    public class Car {
    	// 成员变量 Field
    	private String name;
    	private String color;
    	private int wheelCount;
    	
    	// 构造方法 Constructor
    	public Car() {}
    	
    	public Car(String name, String color, int wheelCount) {
    		this.name = name;
    		this.color = color;
    		this.wheelCount = wheelCount;
    	}
    	
    	/**
    	 * 飙车方法, 如果轮胎个数不为4,提示需要找修理厂
    	 */
    	public void race() {
    		if (4 == wheelCount) {
    			System.out.println("开着" + color + "的" + name + "在秋名山320KM/H飙车");
    		} else {
    			System.out.println("轮胎GG思密达,快去修理厂~~~~");
    		}
    	}
    	
    	// setter 和 getter 方法 Method
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getColor() {
    		return color;
    	}
    
    	public void setColor(String color) {
    		this.color = color;
    	}
    
    	public int getWheelCount() {
    		return wheelCount;
    	}
    
    	public void setWheelCount(int wheelCount) {
    		this.wheelCount = wheelCount;
    	}
    }
    
    5.3 修理厂类代码实现
    package com.qfedu.d;
    
    /*
    修理厂
    	可以看做是一个类
    	成员变量:
    		名字
    		地址
    		联系方式
    	成员方法:
    		修理汽车的方法【重点】
    			该方法是需要一个汽车类的对象
    */
    public class Factory {
    	// 成员变量 Field
    	private String name;
    	private String address;
    	private String tel;
    	
    	// 构造方法 Constructor
    	public Factory() {}
    
    	public Factory(String name, String address, String tel) {
    		this.name = name;
    		this.address = address;
    		this.tel = tel;
    	}
    	
    	/*
    	 * 修理汽车的方法分析
    	 * 权限修饰符
    	 * 		public 
    	 * 返回值类型
    	 * 		void
    	 * 方法名
    	 * 		repair
    	 * 形式参数列表【重点】
    	 * 		1. 这里需要一个汽车作为参数
    	 * 		2. 而且是必要参数
    	 * 		3. 这里需要的是一个汽车类的对象
    	 * 		(Car car) 这里是一个汽车类对象
    	 * 方法声明:
    	 * 		public void repair(Car car);
    	 */
    	/**
    	 * 修理汽车的方法,这里需要的参数是一个汽车Car类的对象
    	 * 
    	 * @param car Car汽车类对象
    	 * @throws InterruptedException 
    	 */
    	public void repair(Car car) throws InterruptedException {
    		// 修理轮胎 通过car类对象获取对应的轮胎个数
    		if (car.getWheelCount() < 4) {
    			System.out.println("轮胎有问题,需要修理");
    			
    			// 修改当前汽车对象的轮胎个数,赋值为4
    			car.setWheelCount(4);
    			
    			// 报错,Ctrl + 1 选择第一个回车
    			Thread.sleep(1000);
    			
    			System.out.println("10W块RMB");
    		} else {
    			System.out.println("你脑子瓦特了~~~~");
    		}
    	}
    
    	// setter和getter方法 Method
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getAddress() {
    		return address;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    	public String getTel() {
    		return tel;
    	}
    
    	public void setTel(String tel) {
    		this.tel = tel;
    	}	
    }
    
    
    5.4 主方法
    package com.qfedu.d;
    
    public class Demo {
    	public static void main(String[] args) throws InterruptedException {
    		/*
    		 * 同package内可以直接使用对应的Class 类
    		 */
    		Car car = new Car("领克03+", "黑色", 4);
    		
    		for (int i = 0; i < 10; i++) {
    			car.race();
    			
    			/*
    			 *  让程序暂停0.5S,存在一个红色的报错,这里Ctrl + 1
    			 *  快速修复,选择第一个,不要问为什么
    			 */
    			Thread.sleep(500);
    		}
    		
    		Thread.sleep(1000);
    		
    		System.out.println("轮胎boom~~~~~");
    		// 轮胎爆炸一个
    		car.setWheelCount(3);
    		
    		Thread.sleep(1000);
    		car.race();
    		
    		// 现在需要一个修理厂对象
    		Factory factory = new Factory();
    		
    		factory.setAddress("海航中路60号");
    		factory.setName("骚磊汽修");
    		factory.setTel("138后面随便");
    		
    		/*
    		 * 这里需要通过Factory类对象,调用repair方法,修理汽车Car类的对象
    		 * 方法参数是一个Car类的对象
    		 */
    		factory.repair(car);
    		
    		for (int i = 0; i < 10; i++) {
    			car.race();
    			
    			/*
    			 *  让程序暂停0.5S,存在一个红色的报错,这里Ctrl + 1
    			 *  快速修复,选择第一个,不要问为什么
    			 */
    			Thread.sleep(500);
    		}
    	}
    }
    
  • 相关阅读:
    LeetCode 227. Basic Calculator II
    LeetCode 224. Basic Calculator
    LeetCode 103. Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 169. Majority Element
    LeetCode 145. Binary Tree Postorder Traversal
    LeetCode 94. Binary Tree Inorder Traversal
    LeetCode 144. Binary Tree Preorder Traversal
  • 原文地址:https://www.cnblogs.com/raising/p/12798964.html
Copyright © 2011-2022 走看看