面向对象编程中,子类可以覆盖父类的方法,但有时你可能需要在子类中调用被覆盖的父类方法。这通常有几种不同的方法,具体取决于你使用的编程语言。以下是一些常见编程语言中调用父类被覆盖方法的方式:
### Java
在Java中,可以使用`super`关键字来调用父类的方法:
```
ocation 对象
location 对象包含有关当前 URL 的信息。
location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问。
<html>
<head>
<script type="text/javascript">
function currLocation()
{
alert(window.location);
}
function newLocation()
{
window.location="/index.html";
}
</script>
</head>
<body>
<input type="button" onclick="currLocation()" value="显示当前的 URL">
<input type="button" onclick="newLocation()" value="改变 URL">
</body>
</html>
location 对象属性
hash 设置或返回从井号 (#) 开始的 URL(锚)。
host 设置或返回主机名和当前 URL 的端口号。
hostname 设置或返回当前 URL 的主机名。
href 设置或返回完整的 URL。
pathname 设置或返回当前 URL 的路径部分。
port 设置或返回当前 URL 的端口号。
protocol 设置或返回当前 URL 的协议。
search 设置或返回从问号 (?) 开始的 URL(查询部分)。
http://example.com:1234/test/test.htm#part2:
hash: #part2
host:example.com:1234
hostname:example.com
href:http://example.com:1234/test.htm#part2
pathname:/test/test.htm
port:1234
protocol:http:
假设当前的 URL 是: http://www.w3school.com.cn/tiy/t.asp?f=hdom_loc_search
search:?f=hdom_loc_search
<script type="text/javascript">
document.write(location.host);
</script>
输出:example.com:1234
location 对象方法
assign() 加载新的文档。
reload() 重新加载当前文档。
replace() 用新的文档替换当前文档。
assign() 方法可加载一个新的文档。
location.assign(URL)
<html>
<head>
<script type="text/javascript">
function newDoc()
{
window.location.assign("http://www.w3school.com.cn");
}
</script>
</head>
<body>
<input type="button" value="Load new document" onclick="newDoc()" />
</body>
</html>
reload() 方法用于重新加载当前文档。
location.reload(bool)
参数如果是false则从浏览器的缓存中重载,如果为true则从服务器上重载,默认值为false;
<html>
<head>
<script type="text/javascript">
function reloadPage(){
window.location.reload();
}
</script>
</head>
<body>
<input type="button" value="Reload page" onclick="reloadPage()" />
</body>
</html>
replace() 方法可用一个新文档取代当前文档。
location.replace(newURL)
replace() 方法不会在 History 对象中生成一个新的记录。当使用该方法时,新的 URL 将覆盖 History 对象中的当前记录。
<html>
<head>
<script type="text/javascript">
function replaceDoc(){
window.location.replace("http://www.w3school.com.cn");
}
</script>
</head>
<body>
<input type="button" value="Replace document" onclick="replaceDoc()" />
</body>
</html>
实例:ThinkPHP框架
<a href='{:U('Circle/circleDetail',array('id'=>$vo['share_id']))}'> <!-- 此种方式会影响页面显示效果 -->
<div class="item item-avatar item-button-right">
<img src="{$vo.head_pic}">
<h2>{$vo.user_name}</h2>
<p>{:friend_date($vo['public_time'])}</p>
<i class=" button button-icon button-outline button-assertive" onclick="setCollection(this, '{$vo.share_id}', '{$user.user_id}')">{$vo.isCollection}</i>
</div>
</a>
代替a链接功能
原型链是ES5中实现继承的主要手段, 因此相对比较重要, 我们需要深入理解原型链.
先来回顾一下构造函数、原型和实例的关系:
思考如下情况:
有些抽象, 我们通过代码来理解:
// 创建Person构造函数
function Person() {
}
// 设置Animal的原型
Person.prototype = {
}
我们将代码修改成原型链的形式:
// 1.创建Animal的构造函数
function Animal() {
this.animalProperty = "Animal"
}
// 2.给Animal的原型中添加一个方法
Animal.prototype.animalFunction = function () {
alert(this.animalProperty)
}
// 3.创建Person的构造函数
function Person() {
this.personProperty = "Person"
}
// 4.给Person的原型对象重新赋值
Person.prototype = new Animal()
// 5.给Person添加属于自己的方法
Person.prototype.personFunction = function () {
alert(this.personProperty)
}
// 6.创建Person的实例
var person = new Person()
person.animalFunction()
person.personFunction()
代码解析:
画图解析可能更加清晰:
当代码执行到第3步(上面代码的序号)的时候, 如图所示:
img
当代码执行第4步(上面代码的序号)时, 发生了如图所示的变化
img
代码继续执行
img
代码继续执行, 我们创建了一个Person对象
原型链简单总结:
如果我们希望确定原型和实例之间的关系, 有两种方式:
instanceof操作符
// instanceof
alert(person instanceof Object) // true
alert(person instanceof Animal) // true
alert(person instanceof Person) // true
isPrototypeOf()函数
// isPrototypeOf函数
alert("isPrototypeOf函数函数")
alert(Object.prototype.isPrototypeOf(person)) // true
alert(Animal.prototype.isPrototypeOf(person)) // true
alert(Person.prototype.isPrototypeOf(person)) // true
添加新的方法
错误代码引起的代码:
// 1.定义Animal的构造函数
function Animal() {
this.animalProperty = "Animal"
}
// 2.给Animal添加方法
Animal.prototype.animalFunction = function () {
alert(this.animalProperty)
}
// 3.定义Person的构造函数
function Person() {
this.personProperty = "Person"
}
// 4.给Person添加方法
Person.prototype.personFunction = function () {
alert(this.personProperty)
}
// 5.给Person赋值新的原型对象
Person.prototype = new Animal()
// 6.创建Person对象, 并且调用方法
var person = new Person()
person.personFunction() // 不会有任何弹窗, 因为找不到该方法
代码解析:
总结
原型链对于继承来说:
原型链存在的问题:
引用类型的问题代码:
// 1.定义Animal的构造函数
function Animal() {
this.colors = ["red", "green"]
}
// 2.给Animal添加方法
Animal.prototype.animalFunction = function () {
alert(this.colors)
}
// 3.定义Person的构造函数
function Person() {
this.personProperty = "Person"
}
// 4.给Person赋值新的原型对象
Person.prototype = new Animal()
// 5.给Person添加方法
Person.prototype.personFunction = function () {
alert(this.personProperty)
}
// 6.创建Person对象, 并且调用方法
var person1 = new Person()
var person2 = new Person()
alert(person1.colors) // red,green
alert(person2.colors) // red,green
person1.colors.push("blue")
alert(person1.colors) // red,green,blue
alert(person2.colors) // red,green,blue
代码解析:
原型链的其他问题:
为了解决原型链继承中存在的问题, 开发人员提供了一种新的技术: constructor stealing(有很多名称: 借用构造函数或经典继承或伪造对象), steal是偷窃的意思, 但是这里可以翻译成借用.
经典继承的做法非常简单: 在子类型构造函数的内部调用父类型构造函数.
经典继承代码如下:
// 创建Animal的构造函数
function Animal() {
this.colors = ["red", "green"]
}
// 创建Person的构造函数
function Person() {
// 继承Animal的属性
Animal.call(this)
// 给自己的属性赋值
this.name = "Coderwhy"
}
// 创建Person对象
var person1 = new Person()
var person2 = new Person()
alert(person1.colors) // red,greem
alert(person2.colors) // red,greem
person1.colors.push("blue")
alert(person1.colors) // red,green,blue
alert(person2.colors) // red,green
代码解析:
这个时候, 我们也可以传递参数, 修改上面的代码:
// 创建Animal构造函数
function Animal(age) {
this.age = age
}
// 创建Person构造函数
function Person(name, age) {
Animal.call(this, age)
this.name = name
}
// 创建Person对象
var person = new Person("Coderwhy", 18)
alert(person.name)
alert(person.age)
经典继承的问题:
回顾原型链和经典继承:
如果你认识清楚了上面两种实现继承的方式存在的问题, 就可以很好的理解组合继承了.
组合继承(combination inheritance, 有时候也称为伪经典继承), 就是将原型链和经典继承组合在一起, 从而发挥各自的优点.
组合继承:
组合继承的代码:
// 1.创建构造函数的阶段
// 1.1.创建Animal的构造函数
function Animal(age) {
this.age = age
this.colors = ["red", "green"]
}
// 1.2.给Animal添加方法
Animal.prototype.animalFunction = function () {
alert("Hello Animal")
}
// 1.3.创建Person的构造函数
function Person(name, age) {
Animal.call(this, age)
this.name = name
}
// 1.4.给Person的原型对象重新赋值
Person.prototype = new Animal(0)
// 1.5.给Person添加方法
Person.prototype.personFunction = function () {
alert("Hello Person")
}
// 2.验证和使用的代码
// 2.1.创建Person对象
var person1 = new Person("Coderwhy", 18)
var person2 = new Person("Kobe", 30)
// 2.2.验证属性
alert(person1.name + "-" + person1.age) // Coderwhy,18
alert(person2.name + "-" + person2.age) // Kobe,30
// 2.3.验证方法的调用
person1.animalFunction() // Hello Animal
person1.personFunction() // Hello Person
// 2.4.验证引用属性的问题
person1.colors.push("blue")
alert(person1.colors) // red,green,blue
alert(person2.colors) // red,green
代码解析:
组合继承是JavaScript最常用的继承模式之一.
组合继承存在什么问题呢?
怎么解决呢?
原文来自:https://mp.weixin.qq.com/s?__biz=Mzg5MDAzNzkwNA==&mid=2247483876&idx=1&sn=d9241b9be5462019dbff10fb539e593f&chksm=cfe3f21bf8947b0db29ef6cc5c85acb7d2e60ecc6fb110d897db65af09962a5f34f334c4a4b0&cur_album_id=1566035091556974596&scene=189#wechat_redirect,如有侵权,请联系删除;
*请认真填写需求信息,我们会在24小时内与您取得联系。