java like a_java is-a、has-a和like-a、组合、聚合和继承 两组概念的区别

本文探讨了Java中继承(isa)与组合(has-a)的关系,如何在类设计时判断是采用继承还是组合。通过实例说明了两者在概念上的区别,以及在实际编程中的应用场景。强调了在设计时组合的灵活性,并给出了生活中的例子来帮助理解这两种关系。

is a 代表的是类之间的继承关系,比如PC机是计算机,工作站也是计算机。PC机和工作站是两种不同类型的计算机,但都继承了计算机的共同特性。因此在用 Java语言实现时,应该将PC机和工作站定义成两种类,均继承计算机类。

has a 代表的是对象和它的成员的从属关系。同一种类的对象,通过它们的属性的不同值来区别。比如一台PC机的操作系统是Windows,另一台PC机的操作系统是Linux。操作系统是PC机的一个成员变量,根据这一成员变量的不同值,可以区分不同的PC机对象。

再比如张三和李四都是人,但他们的名字不一样,可以以此区分这两个具体的人。名字应该作为人的成员变量。如果将名字叫“张三”的人和名字叫“李四”的人分别定义成两个类,均继承“人”这个类,显然是不合理的。

is a 的意识是如果A是B,那么B就是A的基类。

比如:等边三角形是三角形,所以三角形就是等边三角形的基类。

has a 是如果A中有B,那么,B就是A的组成部分

如果你确定两件对象之间是is-a的关系,那么此时你应该使用继承;比如菱形、圆形和方形都是形状的一种,那么他们都应该从形状类继承而不是聚合。

如果你确定两件对象之间是has-a的关系,那么此时你应该使用聚合;比如电脑是由显示器、CPU、硬盘等组成的,那么你应该把显示器、CPU、硬盘这些类聚合成电脑类,而不是从电脑类继承。

===============================================================

现在更多的是强调继承,好像处处都想到继承,其实,组合更加的灵活,应首先考虑组合。

组合是在一类类中引用另一个类。生成另一个类的实例。而继承只是继承了父类的变量和方法。

区别:使用组合可以用到另一个类中私有的变量和方法,而继承就不可以用到父类的私有的变量和方法了,他们都有各自的好处,要灵活的运用。

聚合关系,当A创建的时候,B不一定创建;当A消亡时,B不一定消亡。

class A{   private  B;}

class B{....}

组合关系,当创建一个A对象时,也会创建一个B对象;当A对象消亡时,作为A的属性的B对象也会消亡。

class A{private b=new B();....}

class B{....}

继承是“is   a", 组合是“like   a"

组合关系是:局部类和整体类的关系

包括

从整体类到局部类的分解过程

从局部类到整体类的组合过程

继承关系:父类和子类的关系

包括

从子类到父类的抽象过程,父类是基础类,拥有基本的变量和方法。

从父类到子类的扩展过程

聚合(继承)代码如下:

public class Me extends ZhouJieLun

{

public static void main(String[] args){new Me().singing() ;//打印出“我边吃橄榄边唱歌”}

}

另外一个办法,就是组合复用:

public class Me

{

public void singing(){new ZhouJieLun().singing() ;//打印出“我边吃橄榄边唱歌”}

}

这样举例应该挺清楚了吧

组合与聚合的关系:组合和聚合是有很大区别的,这个区别不是在形式上,而是在本质上:

比如A类中包含B类的一个引用b,当A类的一个对象消亡时,b这个引用所指向的对象也同时消亡(没有任何一个引用指向它,成了垃圾对象),这种情况叫做组合,反之b所指向的对象还会有另外的引用指向它,这种情况叫聚合。

在实际写代码时,组合方式一般会这样写:

A类的构造方法里创建B类的对象,也就是说,当A类的一个对象产生时,B类的对象随之产生,当A类的这个对象消亡时,它所包含的B类的对象也随之消亡。

聚合方式则是这样:

A类的对象在创建时不会立即创建B类的对象,而是等待一个外界的对象传给它,传给它的这个对象不是A类创建的。

现实生活中:人和手,脚是组合关系,因为当人死亡后人的手也就不复存在了。人和他的电脑是聚合关系

sql语法错误,信息: mismatched input '(' expecting ')'(line 4, pos 22) == SQL == with huilv as ( -- 所有年月汇率 select CONCAT(CAST(t2.year AS STRING) as year, LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t2.exchange_rate as qty ----------------------^^^ FROM dm_sever_label_nct t0 left join ods_srm_pu_acceptance_item t1 ON t1.item_no = t0.item_no and t1.has_red = 0 left JOIN ods_srm_pu_acceptance t2 ON t1.acpt_id = t2.id AND t2.dr = 0 AND t2.latest = 1 and t2.source_type='InStorage' and t2.std_currency = 'CNY' and t2.orig_currency = 'USD' where t1.item_no is not null and t2.exchange_rate is not null group by CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')), t2.exchange_rate order by CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) ), huilv_data as ( -- 所有内存条代码汇率 select t0.item_no, '汇率' as data_type, t2.year as cal_year, t2.cal_date, t2.qty FROM dm_sever_label_nct t0 left join huilv t2 where t0.item_no is not null and t2.qty is not null ), gbs as ( -- 所有内存条代码GB单位 select main_ida.text_field_ooil5pe9 as item_no, main_ida.text_field_2lnermdr as qty from ods_ida_t_app0896034115789864961_m1192517514695749632 main_ida where main_ida.id = (select max(ida.id) from ods_ida_t_app0896034115789864961_m1192517514695749632 ida where ida.text_field_ooil5pe9 = main_ida.text_field_ooil5pe9) ), ls_jl_gb as ( -- 历史进料GB select t0.item_no, '历史进料GB' as data_type, t2.year as cal_year, CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t1.acpt_qty*gb.qty as qty FROM dm_sever_label_nct t0 left join gbs gb on t0.item_no = gb.item_no left join ods_srm_pu_acceptance_item t1 ON t1.item_no = t0.item_no and t1.has_red = 0 left JOIN ods_srm_pu_acceptance t2 ON t1.acpt_id = t2.id AND t2.dr = 0 AND t2.latest = 1 and t2.source_type='InStorage' and t2.std_currency = 'CNY' and t2.orig_currency = 'USD' where gb.item_no is not null and t1.item_no is not null and t2.id is not null ), ls_jl_sl as ( -- 历史进料数量 select t0.item_no, '历史进料数量(条)' as data_type, t2.year as cal_year, CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t1.acpt_qty as qty FROM dm_sever_label_nct t0 left join ods_srm_pu_acceptance_item t1 ON t1.item_no = t0.item_no and t1.has_red = 0 left JOIN ods_srm_pu_acceptance t2 ON t1.acpt_id = t2.id AND t2.dr = 0 AND t2.latest = 1 and t2.source_type='InStorage' and t2.std_currency = 'CNY' and t2.orig_currency = 'USD' ), ls_jl_dj_cny as ( -- 历史进料单价 select t0.item_no, '历史进料单价(CNY/条)' as data_type, t2.year as cal_year, CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t1.old_std_price as qty FROM dm_sever_label_nct t0 left join ods_srm_pu_acceptance_item t1 ON t1.item_no = t0.item_no and t1.has_red = 0 left JOIN ods_srm_pu_acceptance t2 ON t1.acpt_id = t2.id AND t2.dr = 0 AND t2.latest = 1 and t2.source_type='InStorage' and t2.std_currency = 'CNY' and t2.orig_currency = 'USD' ), ls_jl_dj_usd as ( -- 历史进料单价 select t0.item_no, '历史进料单价(USD/条)' as data_type, t2.year as cal_year, CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t1.old_orig_price as qty FROM dm_sever_label_nct t0 left join ods_srm_pu_acceptance_item t1 ON t1.item_no = t0.item_no and t1.has_red = 0 left JOIN ods_srm_pu_acceptance t2 ON t1.acpt_id = t2.id AND t2.dr = 0 AND t2.latest = 1 and t2.source_type='InStorage' and t2.std_currency = 'CNY' and t2.orig_currency = 'USD' ), ls_jl_je as ( -- 历史进料金额 select t0.item_no, '历史进料金额(CNY)' as data_type, t2.year as cal_year, CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t1.acpt_std_amount as qty FROM dm_sever_label_nct t0 left join ods_srm_pu_acceptance_item t1 ON t1.item_no = t0.item_no and t1.has_red = 0 left JOIN ods_srm_pu_acceptance t2 ON t1.acpt_id = t2.id AND t2.dr = 0 AND t2.latest = 1 and t2.source_type='InStorage' and t2.std_currency = 'CNY' and t2.orig_currency = 'USD' ), ls_jl_dj_cnygb as ( -- 历史进料单价 select t0.item_no, '历史进料单价(CNY/GB)' as data_type, t2.year as cal_year, CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t0.qty/t1.qty as qty FROM ls_jl_dj_cny t0 left join gbs t1 ON t1.item_no = t0.item_no and t1.cal_date = t0.cal_date where t1.item_no is not null ), ls_jl_dj_usdgb as ( -- 历史进料单价 select t0.item_no, '历史进料单价(USD/GB)' as data_type, t2.year as cal_year, CONCAT(CAST(t2.year AS STRING),LPAD (CAST(t2.month AS STRING), 2, '0')) as cal_date, t0.qty/t1.qty as qty FROM ls_jl_dj_usd t0 left join gbs t1 ON t1.item_no = t0.item_no and t1.cal_date = t0.cal_date where t1.item_no is not null ), ls_ck_dj_cny as ( select t0.item_no, '历史出库单价(CNY/条)' as data_type, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 4) as cal_year, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) as cal_date, t1.item_cost as qty FROM dm_sever_label_nct t0 left join edw_sup_mfg_material_transactions t1 on t1.inventory_item_code = t0.item_no and t1.transaction_source_type_name = '任务或计划' and t1.subinventory_code not like '%NX%' ), ls_ck_dj_usd as ( select t0.item_no, '历史出库单价(USD/条)' as data_type, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 4) as cal_year, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) as cal_date, t1.item_cost/hl.qty as qty FROM dm_sever_label_nct t0 left join huilv_data hl on t0.item_no = hl.item_no left join edw_sup_mfg_material_transactions t1 on t1.inventory_item_code = t0.item_no and t1.transaction_source_type_name = '任务或计划' and t1.subinventory_code not like '%NX%' and SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) = hl.cal_date where t0.item_no is not null and t1.item_cost is not null ), ls_ck_je as ( select t0.item_no, '历史出库金额(CNY)' as data_type, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 4) as cal_year, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) as cal_date, sum(t1.outstock_money) as qty FROM dm_sever_label_nct t0 left join edw_sup_mfg_material_transactions t1 on t1.inventory_item_code = t0.item_no and t1.transaction_source_type_name = '任务或计划' and t1.subinventory_code not like '%NX%' where t1.inventory_item_code is not null group by t0.item_no, t1.p_date ), ls_ck_dj_cnygb as ( select t0.item_no, '历史出库单价GB(CNY/GB)' as data_type, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 4) as cal_year, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) as cal_date, t0.qty/t1.qty as qty FROM ls_ck_dj_cny t0 left join gbs t1 on t0.item_no = t1.item_no and t0.cal_date = t1.cal_date where t1.item_no is not null ), ls_ck_dj_usdgb as ( select t0.item_no, '历史出库单价GB(USD/GB)' as data_type, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 4) as cal_year, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) as cal_date, t0.qty/t1.qty as qty FROM ls_ck_dj_usd t0 left join gbs t1 on t0.item_no = t1.item_no and t0.cal_date = t1.cal_date where t1.item_no is not null ), ls_ck_sl as ( select t0.item_no, '历史出库数量(条)' as data_type, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 4) as cal_year, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) as cal_date, sum(t1.outstock_qty) as qty FROM dm_sever_label_nct t0 left join edw_sup_mfg_material_transactions t1 on t1.inventory_item_code = t0.item_no and t1.transaction_source_type_name = '任务或计划' and t1.subinventory_code not like '%NX%' where t1.inventory_item_code is not null group by t0.item_no, t1.p_date ), ls_ck_gb as ( select t0.item_no, '历史出库GB' as data_type, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 4) as cal_year, SUBSTR(REPLACE(t1.p_date, '-', ''), 1, 6) as cal_date, sum(t1.outstock_qty*gb.qty) as qty FROM dm_sever_label_nct t0 left join gbs gb on t0.item_no = gb.item_no left join edw_sup_mfg_material_transactions t1 on t1.inventory_item_code = t0.item_no and t1.transaction_source_type_name = '任务或计划' and t1.subinventory_code not like '%NX%' where t1.inventory_item_code is not null group by t0.item_no, t1.p_date ), main_data as ( select * from huilv_data --汇率 UNION select * from ls_jl_sl --历史进料条 UNION select * from ls_jl_gb --历史进料GB UNION select * from ls_jl_je --历史进料金额 UNION select * from ls_jl_dj_cnygb --历史进料单价元/GB UNION select * from ls_jl_dj_cny --历史进料单价元/条 UNION select * from ls_jl_dj_usdgb --历史进料单价USD/GB UNION select * from ls_jl_dj_usd --历史进料单价USD/条 UNION select * from ls_ck_sl --历史出库条 UNION select * from ls_ck_gb --历史出库gb UNION select * from ls_ck_je --历史出库金额 UNION select * from ls_ck_dj_cny --历史出库单价元/条 UNION select * from ls_ck_dj_cnygb --历史出库单价元/GB UNION select * from ls_ck_dj_usdgb --历史出库单价usd/GB UNION select * from ls_ck_dj_usd --历史出库单价USD/条 ) INSERT OVERWRITE TABLE dm_server_price_stock_202512 select nct.item_no, '' as item_name, nct.class1, nct.class2, nct.item_bm as bm, '' as jixing, nct.kehu, nct.is_china, nct.is_zte, nct.item_pp, main.data_type, main.cal_year, main.cal_date, main.qty, 0 as num_other1, 0 as num_other2, 0 as num_other3, 0 as num_other4, '' as num_other1, '' as num_other2, '' as num_other3, '' as num_other4, '' as edw_create_date, '' as edw_last_update, '' as edw_data_source, '' as valid_flag from dm_sever_label_nct nct left join qt0 main on main.item_no = nct.item_no
最新发布
12-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值