java转Js原生,Java到JavaScript的转换

我已经做了很多尝试来理解JavaScript中的OOP都没有成功.

到目前为止,我读过的所有文章都非常混乱,并且没有用JS简洁地解释OOP.

作为理解JavaScript中的OOP的最后尝试,请问有人可以将以下代码转换为JS吗?

public class Base

{

public String firstName; // public scope to simplify

public String lastName; // the same as above

Base(String firstName, String lastName)

{

this.firstName=firstName;

this.lastName=lastName;

}

}

public class Derived extends Base

{

public int age; // public scope to simplify

Derived(String firstName, String lastName, int age)

{

super(firstName, lastName);

this.age=age;

}

}

内部main()

Derived person = new Derived("John", "Doe", 25);

System.out.println("My name is " + person.firstName + " " + person.lastName + " and I have " + person.age + " years old.");

输出:

My name is John Doe and I have 25 years old.

您可以将其转换为JavaScript吗?

另一个问题:我们可以在JavaScript中使用多态性吗?

解决方法:

由于JavaScript是基于原型的,因此没有Class之类的东西.您可以使用构造函数(函数)来创建自定义数据类型并提供原型继承.

function Base (firstName, lastName) {

// same as public in Java

this.firstName = firstName;

this.lastName = lastName;

}

function Derived (firstName, lastName, age) {

// same as calling super class in Java, and you should explicitly bind this

Base.apply(this, arguments);

this.age = age;

}

// same as extends in Java, you just override Derived.prototype with super class prototype and you should explicitly set constructor if you want later check instanceof.

Derived.prototype = Object.create(Base.prototype, {

// if you omit this line than instanceof Derived would be false, since instanceof check by prototype chain constructors.

constructor: {

value: Derived

}

});

var person = new Derived("John", "Doe", 25);

console.log(person instanceof Derived); // true

console.log(person instanceof Base); // true

console.log("My name is " + person.firstName + " " + person.lastName + " and I have " + person.age + " years old.");

关于多态,如果您问的是方法重载,那么就没有这种事情了,但是,由于javascript是弱类型的动态语言,因此您可以通过检查该参数的arguments长度和typeof来获得相同的结果.我相信多态的其他概念与Java中的工作原理相同.

简单例如:

function bar(a, b) {

if (arguments.length === 2) {

var isInts = [].every.call(arguments, function(val) {

return !isNaN(val);

});

if (isInts) {

console.log(a + b);

}

} else if (arguments.length > 2) {

console.log([].join.call(arguments, ""));

}

}

bar(2, 5);

bar("Hello", ", ", "world", "!");

标签:oop,javascript

来源: https://codeday.me/bug/20191030/1964098.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp原生js组件是指在uniapp中使用原生开发语言(如Android的Java或iOS的Objective-C/Swift)编写的组件,可以通过uniapp的插件机制在uniapp项目中调用和使用。这些组件通常需要通过原生插件的方式来实现,例如在Android中编写Java代码,然后通过js调用该组件的方法。这样就可以在uniapp项目中使用原生的功能和特性,扩展应用的能力和功能。比如可以调用计步器API或实现全局悬浮框等需求。因为涉及到原生开发语言的编写和调用,所以对于前端开发者来说可能需要学习和了解一些原生开发的知识和技能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [uniapp原生安卓插件开发之路](https://blog.csdn.net/xj932956499/article/details/106442005)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [uni-app结合原生混合开发](https://blog.csdn.net/u011513460/article/details/112978354)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值