go面向对象 vs c++面向对象
面向对象的四大特性
- 抽象—数据抽象(属性)和过程抽象(方法)
- 封装—限定对外方法,控制数据访问
- 继承—数据共享、代码重用
- 多态—派生对象就是父对象
c++ 抽象和封装
#include <iostream>
using namespace std;class rect {
private:int length; //长度int width; //宽度
public:int area(){return length*width;}rect(int length, int width){this->length = length;this->width = width;}
};int main()
{rect c(10, 10);cout<<"area:"<<c.area()<<"\n";// area:100
}
go 抽象和封装
package main
import "fmt"// 结构体:长方形
type rect struct {length int // 长度weight int // 高度
}// 方法:长方形面积
func (r *rect) area() int {return r.length * r.weight
}// go属性和方法的可见性是包(package)级别
// 如果,将属性名和方法名定义为大写字母开头
// 那么,对其他包(package)可见func main() {// go没有构造函数r := rect{10, 10}fmt.Println("area:", r.area())// area: 100
}
go方法(传值和传址)
package main
import "fmt"// 结构体:长方形
type rect struct {length int // 长度width int // 宽度
}// 方法:增加长度(传值)
func (r rect) increase_length_value() {r.length = r.length + 1
}// 方法:增加长度(传址)
func (r *rect) increase_length_pointer() {r.length = r.length + 1
}// 方法:长方形面积
func (r rect) area() int {return r.length * r.width
}
// 传值:对象副本
// 传址:对象地址
// 1、是否修改对象
// 2、对象副本创建成本func main(){r := rect{length:10, width:10}fmt.Println("area:", r.area())// area: 100r.increase_length_value()fmt.Println("area:", r.area())// area: 100r.increase_length_pointer()fmt.Println("area:", r.area())// area: 110p := &rect{length:10, width:10}fmt.Println("area:", p.area())// area: 100p.increase_length_value()fmt.Println("area:", p.area())// area: 100p.increase_length_pointer()fmt.Println("area:", p.area())//area: 110//可用实例value和pointer调用全部方//编译器自动转换
}
c++ 继承
#include <iostream>
using namespace std;class rect {
private:int length; //长度int width; //宽度
public:int area(){return length*width;}rect(int length, int width){this->length = length;this->width = width;}
};class cube : public rect{
private:int height; //长度
public:cube(int length, int width, int height):rect(length, width) {this->height = height;}int volume(){return this->area() * this->height;}
}int main()
{cube c(10, 10,10);cout<<"area:"<<c.volume()<<"\n";// area:1000
}
go “继承”
package main
import "fmt"// 结构体:长方形
type rect struct {length int // 长度width int // 高度
}// 方法:长方形面积
func (r *rect) area() int {return r.length * r.width
}type cube struct {rect //匿名属性height int //长度
}func (r *cube) volume() int {return r.area() * r.height
}func main() {c := cube{rect{10, 10}, 10}fmt.Println("area:", c.volume())
}
// 编译器负责查找匿名方法,实现类似继承的复用
c++(is-a)
#include <iostream>
using namespace std;class rect {
private:int length; //长度int width; //宽度
public:int area(){return length*width;}rect(int length, int width){this->length = length;this->width = width;}
};class cube : public rect{
private:int height; //长度
public:cube(int length, int width, int height):rect(length, width) {this->height = height;}int volume(){return this->area() * this->height;}
};void print_area(rect r) {cout<<"area is:" << r.area()<<"\n";
}int main()
{rect r(10, 10);print_area(r);// area:100cube c(10, 10,10);print_area(c);// area:100
}
go(has-a)
package main
import "fmt"// 结构体:长方形
type rect struct {length int // 长度width int // 高度
}type cube struct {rect //匿名属性height int //长度
}// 方法:长方形面积
func print_area(r rect) {fmt.Println("area:",r.length * r.width)
}func main() {r := rect{10, 10}print_area(r)//area: 100//c := cube{rect{10, 10}, 10}//print_area(c)// cannot use c (type cube) as type rect// in argument to print_area
}// 真正的多态,派生对象就是父对象
go(fake-is-a)
package main
import "fmt"// 结构体:长方形
type rect struct {length int // 长度width int // 高度
}// 方法:长方形面积
func (r *rect) area() int {return r.length * r.width
}type cube struct {rect //匿名属性height int //长度
}func main() {c := cube{rect{10, 10}, 10}fmt.Println("area:", c.area())//area: 100
}
// 编译器负责查找匿名方法
// 实现类似继承的复用能力
c++(多重继承——属性重复)
#include <iostream>
using namespace std;class plane {
public:int length; //共同属性int width;
};class line {
public:int length; //共同属性string name;
};class combile:public plane, public line{
};int main()
{combile c;c.name = "my name";//c.length = 10;//member 'length' found in multiple //base classes of different typesplane *p;p = &c;p->length = 10;cout<<p->length<<"\n";//10line *l;l = &c;l->length = 5;cout<<l->length<<"\n";//5return 0;
}
// is-a
go(多重复合——属性重复)
package main
import "fmt"// 平面
type plane struct{length int //长度width int //宽度
}// 直线
type line struct {length int // 长度name string // 名称
}type combine struct {lineplane
}func main() {l := line{50, "first line"}p := plane{10,10}c := combine{l, p}fmt.Println("length of c1:", c.name)fmt.Println("length of c1:", c.width)fmt.Println("length of c1:", c.line.length)fmt.Println("length of c1:", c.plane.length)//fmt.Println("length of c1:", c.length) //ambiguous selector c.length
}//has-a
go(is-a)
package main
import "fmt"type flyer interface {fly()
}type bee struct {name string
}type plane struct{name string
}func (b *bird) fly() {fmt.Printf("%s is flying \n", b.name)
}func (p *plane) fly() {fmt.Printf("%s is flying \n", p.name)
}func i_can_fly(f flyer){fmt.Println("i can fly")f.fly()
}func main() {b := bee{"first bee"}i_can_fly(&b)// i can fly// first bee is flyingp := plane{"BY 737"}i_can_fly(&p)// i can fly// BY 737 is flying
}//接口是一个或多个方法签名的集合。
//任何类型的方法集中只要拥有该接口'对应的全部方法'签名。
//就表示它 "实现" 了该接口,无须在该类型上显式声明实现了哪个接口。
//接口只有方法声明,没有实现,没有数据字段。
//接口命名习惯以 er 结尾。// go把struct作为数据和逻辑的结合
// 通过组合(composition),has-a关系来最小化代码重用
// go使用接口interface来建立(type)之间的is-a关系
go interface自动判断
package main
import "fmt"type flyer interface {fly()
}type spider struct {
}func (s *spider) fly() {fmt.Println("spider can fly!")
}type hero interface {save_world()
}type looser struct {
}func (l *looser) save_world() {fmt.Println("i'm a looser ,but i can save the world!")
}type spider_man struct {spiderlooser
}type super_man interface {fly()save_world()
}func welcome_superman(s super_man) {s.save_world()s.fly()fmt.Println("welcome the superman!")}func main() {s := spider{}l := looser{}sp := spider_man{s, l}welcome_superman(&sp)// i'm a looser ,but i can save the world!// spider can fly!// welcome the superman!
}
c++ interface显示判断
#include <iostream>
using namespace std;class flyer {
public:virtual void fly() = 0;
};class hero {
public:virtual void save_world() = 0;
};class super_man {
public:virtual void fly() = 0;virtual void save_world() = 0;
};class spider_man : public flyer, public hero {
public:void fly(){cout<<"i can fly!\n";}void save_world(){cout<<"i can save world!\n";}
};class captain: public super_man {
public:void fly(){cout<<"i can fly!\n";}void save_world(){cout<<"i can save world!\n";}
};void welcome_superman(super_man &s){s.fly();s.save_world();cout<<"welcome the superman!";
}int main()
{//spider_man s;//welcome_superman(s);// note: candidate function not viable:// no known conversion from 'spider_man' // to 'super_man &' captain c;welcome_superman(c);//i can fly!//i can save world!//welcome the superman!return 0;
}
go interface{}
package main
import "fmt"// 结构体:长方形
type rect struct {length int // 长度width int // 高度
}func print_filter(i interface{}){fmt.Println("nogo print:", i)
}func main() {i := 10s := "nogo"r := rect{10, 10}print_filter(i)//nogo print: 10print_filter(s)//nogo print: nogoprint_filter(r)//nogo print: {10 10}
}
参考文章:
https://blog.csdn.net/li_101357/article/details/80205005
https://blog.csdn.net/zhangjg_blog/article/details/18790965
go面向对象 vs c++面向对象相关推荐
- python面向对象的优点_Python面向对象编程——总结面向对象的优点
Python面向对象编程--总结面向对象的优点 一.从代码级别看面向对象 1.在没有学习类这个概念时,数据与功能是分离的 def exc1(host,port,db,charset): conn=co ...
- java基础面向对象_Java基础面向对象
一.面向过程的思想和面向对象的思想 面向对象和面向过程的思想有着本质上的区别,作为面向对象的思维来说,当你拿到一个问题时,你分析这个问题不再是第一步先做什么,第二步再做什么,这是面向过程的思维,你应该 ...
- python面向对象编程的优点-Python面向对象编程——总结面向对象的优点
Python面向对象编程--总结面向对象的优点 一.从代码级别看面向对象 1.在没有学习类这个概念时,数据与功能是分离的 def exc1(host,port,db,charset): conn=co ...
- python完全支持面向对象编程_Python 面向对象编程概要
面向对象三大特性 面向对象的三大特性是指:封装.继承和多态. 封装 封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容. 所以,在使用面向对象的封装特性时,需要: 将内容封装到某处 ...
- php控制器面向对象编程,PHP 面向对象编程(2)
一些内建方法: class Person { public $isAlive = true; function __construct($name) { //这里我们创建了一个name的属性 $thi ...
- Java面向对象的编程⑤面向对象
今日内容:java面向对象 1面向对象的思想 面向对象和面向过程区别: 面向对象是相对于面向过程,面向过程指的功能行为,面向对象指将功能封装对象中,强调的是功能的对象 面向过程:打开门大象放进去关闭门 ...
- python 完全面向对象_python之面向对象
Python Python开发 Python语言 python之面向对象 第一章 面向对象初识 面向对象的三大特性是什么? 抽象.继承.多态. 面向对象第一个优点:* 对相似功能的函数,同一个业务的函 ...
- 【Java开发语言 03】第三章 面向对象编程(面向对象与面向过程+类和对象+类成员一:属性+类成员二:方法+对象的创建和使用+封装和隐藏+构造器+关键字this,package,import)
面向对象编程 1 面向对象与面向过程 1.1 java类及类的成员 2 java语言的基本元素:类和对象 2.1 类的语法格式 2.2 创建Java自定义类 2.3 对象的创建及使用 3 类的成员之一 ...
- 【廖雪峰python总结】python高级特性,函数式编程,面向对象编程,面向对象高级编程
文章目录 python复习总结 python高级特性 函数式编程 面向对象编程 面向对象高级编程 python复习总结 python高级特性 python高级特性 函数式编程 函数式编程 函数式编程的 ...
- PHP什么是面向对象?PHP面向对象小结
本篇文章给大家带来的内容是PHP什么是面向对象?PHP面向对象小结.有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助. 面向对象特性: *重用性 (每个模块都可以在项目中重复使用) *灵活 ...
最新文章
- 【CodeForces 577C】Vasya and Petya’s Game
- ❤️六W字《计算机基础知识》(三)(建议收藏)❤️
- php知识点_php中一些重要的php知识点分享
- scrollbarStyle属性
- MongoDB CookBook读书笔记之导入导出
- 最长公共子序列php,动态规划(最长公共子序列LCS)
- java创建包顺序_Java中包含继承关系时对象的创建与销毁顺序详解(附源码)
- UPC 条形码的介绍及计算校验码
- java程序设计俄罗斯方块_Java俄罗斯方块实现代码
- 海康Ehome协议服务端搭建
- 数字化底层逻辑揭秘!探寻地产工程行业发展新范式
- python-docx教程
- 系统设置中 语言设置,中文或者英文
- MySQL索引面试题六连击
- CAD高版本窗体阵列LISP_[转载]AutoCAD高版本怎么把阵列对话框调出来?
- 笑话大全API 实战项目 开心一笑app
- 广州拟放宽“双一流”高校人才入户门槛
- 完美解决 unbuntu中vim编辑完成后 按ESC毫无反映
- 老王课程学习,第八课
- Redis 分布式锁笔记
热门文章
- (免费使用)爱招飞IsoFace物联网开发工具 Smart 配合工厂自动化自定开发设计各种组态环境
- 班级缴费信息管理系统
- slurm作业调度系统与天河二号的基本命令(新手入门, 以gs和vasp为例)
- linux---vi编辑器
- Lifelong GAN: Continual Learning for Conditional Image Generation
- 写英文商务邮件必备的9大黄金句子
- Star CCM+多孔介质仿真 (一)——仿真流程
- 金证科技 java开发工程师 校招面经(已offer)
- 金证科技 前端开发工程师校招一面面经
- C++ char 转 int