设计模式之面向对象

在进行面向对象设计程序的时候我们一般要考虑到以下的几种面向对象的思想,这样我们才能够享受面向对象编程给我们带来的快乐: - )

 

考虑类

主要是一些名词,在面向对象的编程里我们首相就要观察我们的问题域中有多少东西能够抽象成对象。

属性

在命名时要与开发的具体环境所联系。

方法就能够体现类之间的关系

隐藏(封装)

在计算机相关的工程学中最大的特点就是结构上的重复性,在一个程序里代码的共用是非常普遍的现象。而在面向对象的编程里提供代码共用的思想就是封装。

降低耦合度,它本来该做的事情交给他自己做模块化的思想。

继承:只要能够说通A是一种B就可以考虑用继承

多态:是面向对象当中最难最核心的思想。它的特点是:有继承,有重写,有父类的引用指向子类的对象。最主要的功能是它能够让代码的可扩展性非常好!当想修改一个程序的时候我们能够使用添加的手段去解决那么说明我们的程序的可扩展性很好。

一些朴素原则:

①属性是private

②直接访问与get/set方法的访问区别就在于使用get/set方法能够得到更为精确的控制,比如说在前面加上判断,或者是在后面加上一些后置的操作。或者如果只允许访问的话那么就去掉set方法。

③设计没有绝对的对与错仁者见仁智者见智。

一些设计细节

接口与抽象类

如果脑子里有一个概念但是没有具体的形象的表述那么就用抽象类。

如果是一类事物与几类事物的特征那么就用接口。会飞的,会跑的等等。

不要过于设计

我们要根据编程的情景去设计适当的设计不要一遇到问题就去机械的教条的套用严格的面相对象。

下面是一个用Java语言去描述“老张开车去东北”的面向对象版本(引用马士兵老师的案例 :- )  

①首先明确情境中有什么类

司机类(老张),一辆车(车类),东北(地点类)

②属性

司机:name

车:

地点:name

③方法

司机:开车drive

车:行驶

④封装

所有的属性都用private修饰并根据需求赋予get/set方法

⑤继承

考虑到老张开的不一定是车所以新建一个交通工具类Vihecle,让所有的不同类的交通工具去继承它

⑥多态

因为所有的交通工具都有一个行驶的方法而他们具体的行驶方式又不同所以将Vihecle变成一个抽象类并且含有一个抽象方法go。这样就增加了程序的可扩展性。

以下为代码:

 Address类

package ThinkInOOP;

public class Address {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
	
}


Vihecle类

package ThinkInOOP;

public abstract class Vihecle{
	
	public abstract void go(Address dest);
}


Driver类

package ThinkInOOP;

public class Driver {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void drive(Vihecle v,Address dest) {
		v.go(dest);
	}
	
}


Car类

package ThinkInOOP;

public class Car extends Vihecle{
	
	public void go(Address dest) {
		System.out.println("他一个人去了"+dest.getName());
	}


}


Plane类

package ThinkInOOP;

public class Plane extends Vihecle{

	@Override
	public void go(Address dest) {
		System.out.println("他飞去了"+dest.getName());
	}
	
}


测试类

package ThinkInOOP;

public class Demo {
	public static void main(String[] args) {
		Driver driver = new Driver();
		driver.setName("老张");
		Address dest = new Address();
		dest.setName("东北");
		driver.drive(new Plane(), dest);
	}
	
}

总体类的关系

最后特别感谢马士兵老师: - )


Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Elong_Hu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值