java的cast_Java Class.cast()与强制转换运算符

在我的C ++时代里,学习过有关C风格的强制转换运算符的弊端后,我很高兴首先发现Java 5中java.lang.Class已经获得了一种cast方法。

我以为最终我们有了一种面向对象的处理铸造的方法。

事实证明Class.cast与static_castC ++不同。更像是reinterpret_cast。它不会在预期的地方生成编译错误,而是会推迟到运行时。这是一个演示不同行为的简单测试用例。

package test;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class TestCast

{

static final class Foo

{

}

static class Bar

{

}

static final class BarSubclass

extends Bar

{

}

@Test

public void test ( )

{

final Foo foo = new Foo( );

final Bar bar = new Bar( );

final BarSubclass bar_subclass = new BarSubclass( );

{

final Bar bar_ref = bar;

}

{

// Compilation error

final Bar bar_ref = foo;

}

{

// Compilation error

final Bar bar_ref = (Bar) foo;

}

try

{

// !!! Compiles fine, runtime exception

Bar.class.cast( foo );

}

catch ( final ClassCastException ex )

{

assertTrue( true );

}

{

final Bar bar_ref = bar_subclass;

}

try

{

// Compiles fine, runtime exception, equivalent of C++ dynamic_cast

final BarSubclass bar_subclass_ref = (BarSubclass) bar;

}

catch ( final ClassCastException ex )

{

assertTrue( true );

}

}

}

所以,这些是我的问题。

应该Class.cast()放逐到泛型土地吗?那里有很多合法用途。

Class.cast()使用时,编译器是否应该生成编译错误,并且可以在编译时确定非法条件?

Java是否应提供强制转换运算符作为类似于C ++的语言构造?

优化sql:SELECT we.organization_id ,we.wip_entity_id ,case when wl.line_id is null then we.wip_entity_name else '' end wip_entity_name ,we.primary_item_id ,mtt.transaction_type_name ,mmt.transaction_date ,bd.department_code ,mmt.inventory_item_id ,mmt.subinventory_code ,mta.reference_account ,br.resource_code ,lu2.meaning as line_type_name ,mta.base_transaction_value ,mta.cost_element_id ,flv.meaning as cost_element ,wdj.class_code job_type_code ,ml.meaning job_type_name FROM (select * from gerp.mtl_material_transactions where substr(transaction_date,1,7) >= '2023-06' and transaction_source_type_id = 5) mmt inner join gerp.wip_entities we on mmt.organization_id = we.organization_id inner join gerp.mtl_transaction_accounts mta on mta.transaction_source_id = we.wip_entity_id and mta.transaction_id = mmt.transaction_id and mta.transaction_source_type_id = 5 inner join gerp.mtl_transaction_types mtt on mtt.transaction_type_id = mmt.transaction_type_id inner join mfg_lookups lu2 on lu2.lookup_code = mta.accounting_line_type and lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE' inner join gerp.mtl_system_items_b msi on msi.inventory_item_id = mmt.inventory_item_id and msi.organization_id = mta.organization_id left join gerp.bom_departments bd on bd.department_id = mmt.department_id left join gerp.bom_resources br on br.resource_id = mta.resource_id left join gerp.wip_lines wl on wl.line_id = mmt.repetitive_line_id left join gerp.wip_discrete_jobs wdj on wdj.wip_entity_id = mta.transaction_source_id left join gerp.fnd_lookup_values_vl flv on cast(mta.cost_element_id as string) = flv.lookup_code and flv.lookup_type = 'CST_COST_CODE_TYPE' left join mfg_lookups ml on ml.lookup_code = wdj.job_type and ml.lookup_type = 'WIP_DISCRETE_JOB' 。其中mmt,we,mta,msi,wdj数据量很大
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值