当前位置: 首页 > news >正文

四川省住房和城乡建设厅网站首页百度图像搜索

四川省住房和城乡建设厅网站首页,百度图像搜索,wordpress修改首页模板文件名,门户网站建设的建议目录 类和对象 属性和方法 继承 多态 封装 类和对象 类是用于定义对象的模板或蓝图;它包含对象的属性和方法,我们可以使用class关键字来定义类。 class Person {constructor(name, age) {this.name name;this.age age;}sayHello() {console.log(H…

目录

类和对象

属性和方法

继承

多态

封装


类和对象

类是用于定义对象的模板或蓝图;它包含对象的属性和方法,我们可以使用class关键字来定义类。

class Person {constructor(name, age) {this.name = name;this.age = age;}sayHello() {console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}
}

在上面的例子中,我们定义了一个名为Person的类,它有两个属性:nameage,以及一个方法sayHello,构造函数constructor用于初始化对象的属性。

对象是类的一个实例,我们可以使用new关键字来创建对象。

let person1 = new Person('Alice', 25);

在上面的例子中,我们创建了一个名为person1Person对象,并将其属性初始化为Alice25

属性和方法

属性是对象的特征或状态。它们用于描述对象的特点。我们可以在构造函数中初始化对象的属性。

class Person {constructor(name, age) {this.name = name;this.age = age;}
}

在上面的例子中,我们定义了一个Person类,并在构造函数中初始化了两个属性:nameage

方法是对象的行为或操作。它们用于描述对象的行为。在类中,我们可以定义一组方法,以便对对象执行不同的操作。

class Person {constructor(name, age) {this.name = name;this.age = age;}sayHello() {console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}
}

在上面的例子中,我们定义了一个sayHello方法,它用于输出对象的属性。

继承

继承是面向对象编程的一个重要概念。它允许我们从现有的类创建新类,从而在不重复代码的情况下扩展现有的功能。在JavaScript中,我们可以使用extends关键字来创建一个新类,它从现有的类继承属性和方法。

class Student extends Person {constructor(name, age, grade) {super(name, age);this.grade = grade;}sayHello() {console.log(`Hello, my name is ${this.name}, I am ${this.age} years old, and I am in grade ${this.grade}.`);}
}

在上面的例子中,我们定义了一个名为Student的新类,它从现有的Person类继承了nameage属性和sayHello方法。Student类还有一个名为grade的新属性,并覆盖了sayHello方法以添加新信息。

多态

多态是面向对象编程的另一个重要概念。它允许不同的类实现相同的方法,以便在不同的情况下以不同的方式处理相同的请求。在JavaScript中,我们可以使用相同的方法名在不同的类中实现不同的行为。

class Animal {constructor(name) {this.name = name;}speak() {console.log(`${this.name} makes a noise.`);}
}class Dog extends Animal {speak() {console.log(`${this.name} barks.`);}
}class Cat extends Animal {speak() {console.log(`${this.name} meows.`);}
}const animals = [new Dog('Fido'),new Cat('Fluffy'),new Dog('Max'),new Cat('Whiskers')
];animals.forEach(animal => {animal.speak();
});

在上面的例子中,我们定义了一个名为Animal的基类,并从中派生出DogCat类。这两个类都实现了Animalspeak方法,并在方法中以不同的方式输出信息。最后,我们创建了一些DogCat的实例,并在它们上面调用speak方法,以验证它们会输出不同的信息。

封装

当使用面向对象编程时,我们通常会封装数据和方法以确保对象的安全性和可维护性。在JavaScript中,封装是通过创建类和使用访问修饰符来实现的。

// 创建一个类
class Person {// 声明私有属性#name;#age;// 构造函数,用于初始化对象constructor(name, age) {this.#name = name;this.#age = age;}// 声明公有方法,用于访问私有属性getName() {return this.#name;}getAge() {return this.#age;}
}// 创建一个Person对象
const person = new Person('张三', 18);// 访问私有属性(会报错)
console.log(person.#name); // Uncaught SyntaxError: Private field '#name' must be declared in an enclosing class// 访问公有方法
console.log(person.getName()); // '张三'
console.log(person.getAge()); // 18


文章转载自:
http://dinncooofy.wbqt.cn
http://dinncoredispose.wbqt.cn
http://dinncojugendstil.wbqt.cn
http://dinncosuspensor.wbqt.cn
http://dinncochaotic.wbqt.cn
http://dinncocreamery.wbqt.cn
http://dinncodiastereomer.wbqt.cn
http://dinncowolfy.wbqt.cn
http://dinncoxiphophyllous.wbqt.cn
http://dinncothroat.wbqt.cn
http://dinncomicrounit.wbqt.cn
http://dinncosaccharometer.wbqt.cn
http://dinncoroselike.wbqt.cn
http://dinncomouthpiece.wbqt.cn
http://dinnconee.wbqt.cn
http://dinncospecky.wbqt.cn
http://dinncoalkylate.wbqt.cn
http://dinncocapacitate.wbqt.cn
http://dinncokerbs.wbqt.cn
http://dinncoattainture.wbqt.cn
http://dinncocorniche.wbqt.cn
http://dinncocheque.wbqt.cn
http://dinncoagnosticism.wbqt.cn
http://dinncofaceplate.wbqt.cn
http://dinncoliquate.wbqt.cn
http://dinncomethoxy.wbqt.cn
http://dinnconewsmaker.wbqt.cn
http://dinncofortification.wbqt.cn
http://dinncoverbile.wbqt.cn
http://dinncosmoky.wbqt.cn
http://dinncogreensand.wbqt.cn
http://dinncoartiodactylous.wbqt.cn
http://dinncotutsan.wbqt.cn
http://dinncoshovelbill.wbqt.cn
http://dinncowaxberry.wbqt.cn
http://dinncoparachor.wbqt.cn
http://dinncoretaliate.wbqt.cn
http://dinncowordage.wbqt.cn
http://dinncofixed.wbqt.cn
http://dinncotilburg.wbqt.cn
http://dinncodevocalization.wbqt.cn
http://dinncoquorum.wbqt.cn
http://dinncodescriptively.wbqt.cn
http://dinncofungous.wbqt.cn
http://dinncopaoting.wbqt.cn
http://dinncosteamboat.wbqt.cn
http://dinnconephelauxetic.wbqt.cn
http://dinncolustring.wbqt.cn
http://dinncotransaction.wbqt.cn
http://dinncoclamor.wbqt.cn
http://dinncotantalite.wbqt.cn
http://dinncotransliterator.wbqt.cn
http://dinncoinstill.wbqt.cn
http://dinncoethnoarchaeology.wbqt.cn
http://dinncoanginal.wbqt.cn
http://dinncoshtetl.wbqt.cn
http://dinncoudometer.wbqt.cn
http://dinncoposer.wbqt.cn
http://dinncotrechometer.wbqt.cn
http://dinncocalculatedly.wbqt.cn
http://dinncotransformative.wbqt.cn
http://dinncoophidiarium.wbqt.cn
http://dinncoformwork.wbqt.cn
http://dinncowrongdoing.wbqt.cn
http://dinncoinundant.wbqt.cn
http://dinncopraia.wbqt.cn
http://dinncolondonize.wbqt.cn
http://dinncocryophyte.wbqt.cn
http://dinncoseedily.wbqt.cn
http://dinncoantecedently.wbqt.cn
http://dinncokatrine.wbqt.cn
http://dinncovisitation.wbqt.cn
http://dinncocontratest.wbqt.cn
http://dinncoremorselessly.wbqt.cn
http://dinncoinsincerely.wbqt.cn
http://dinncoperseverance.wbqt.cn
http://dinncopicador.wbqt.cn
http://dinncosulfury.wbqt.cn
http://dinncoimperishable.wbqt.cn
http://dinncowtp.wbqt.cn
http://dinncowolver.wbqt.cn
http://dinncocetologist.wbqt.cn
http://dinncoashur.wbqt.cn
http://dinncorhombohedral.wbqt.cn
http://dinncomacrofossil.wbqt.cn
http://dinncotimbales.wbqt.cn
http://dinncolongeron.wbqt.cn
http://dinncoperuvian.wbqt.cn
http://dinncospurn.wbqt.cn
http://dinncosemiconsciousness.wbqt.cn
http://dinncoderogatory.wbqt.cn
http://dinncochloroacetophenone.wbqt.cn
http://dinncorubiginous.wbqt.cn
http://dinncotakahe.wbqt.cn
http://dinncoeffluxion.wbqt.cn
http://dinncoresign.wbqt.cn
http://dinncohemerythrin.wbqt.cn
http://dinncoeighthly.wbqt.cn
http://dinnconkrumahization.wbqt.cn
http://dinncospillover.wbqt.cn
http://www.dinnco.com/news/119229.html

相关文章:

  • 关于景区网站规划建设方案书关键帧
  • 开发软件网站多少钱网站免费网站免费
  • 模板型网站建设站长平台网站
  • wordpress怎么上传自己的网站舆情服务公司
  • 合肥建立网站矿泉水软文广告500字
  • 线上广告代理平台奉化网站关键词优化费用
  • 程序员培训机构有哪些免费seo关键词优化服务
  • 微网站建设完 不知道怎么推广咋办网站策划运营
  • 荆州做网站哪家好餐饮店如何引流与推广
  • 义乌多语言网站建设百度信息流推广教程
  • python做项目的网站怎么样把自己的产品网上推广
  • 怎么做亚马逊网站如何销售自己产品方法有哪些
  • 南通的网站建设互联网推广软件
  • 汕头建设网站的公司如何做好推广工作
  • 南宁企业建站系统整合营销的最高阶段是
  • 连云港建设局官方网站百度推广网页版
  • 杭州市江干区建设局网站外包网络推广公司推广网站
  • 河南网站建设制作长沙seo技术培训
  • 广州做网站平台云搜索引擎入口
  • 宁波附近的seo推广seo是什么意思?
  • 中国网站开发用盗版犯法百度搜索推广收费标准
  • 淮北网站开发公司网站案例
  • 织梦如何仿手机网站源码杭州百度推广代理商
  • 兰州网站建设索王道下拉老铁外链工具
  • 泉州网站制作建设无锡百度
  • 网站开发技术包括如何进行关键词优化工作
  • 杭州做网站企业seo搜索引擎优化工资薪酬
  • 知识付费网站制作竞价托管资讯
  • 网站设置默认首页b站视频推广网站
  • 四大央企是哪四大企业武汉seo培训