一.特征
面向对象的三大特征:封装 继承 多态
二 封装
1.1封装含义
隐藏对象的属性,对外只提供可以访问属性的方法,可提高类的安全性
访问修饰符 本类,本包,其他包,子类
public 共有的 任何位置都可以访问
private 私有的 只允许本类访问
export class Pet{
//访问修饰符 本类,本包,其他包,子类
//public 共有的 任何位置都可以访问
public id:number
//default 默认
name:string
private sex:string
b(){
console.log('默认');
}
//私有的 只允许本类访问
private a(){
console.log('私有');
}
constructor(id:number, name:string,sex:string){
this.id=id
this.name=name
this.sex=sex
}
}
1.2封装全部书写方式:
先用构造函数写出我们要写的名称
class Pets{
constructor(id:number,name:string,sex:string,helth:number,love:number){
this.id=id
this.name=name
this.sex=sex
this.helth=helth
this.love=love
}
私有属性
private id:number
private name:string
private sex:string
private helth:number
private love:number
提供一个共有的方法来访问属性 getter/setter
public getId():number{
return this.id;
}
public getName():string{
return this.name;
}
public getSex():string{
return this.sex;
}
public getHelth():number{
return this.helth;
}
public getLove():number{
return this.love;
}
set 设置/赋值
public setId(id:number){
this.id=id
}
public setName(name:string){
this.name=name
}
public setSex(sex:string){
this.sex=sex
}
public setHelth(helth:number){
this.helth=helth
}
public setLove(love:number){
this.love=love
}
}
调用函数修改属性的值
let p:Pets=new Pets(1,'笨笨','公',100,100);
p.setHelth(90)
必须使用get函数访问属性
console.log(`序号:${p.getId()},昵称:${p.getName()}`);
打印结果:
三. 继承
继承的关键字 extends 子类继承父类的属性和方法
通过expoer导出 用import导入
3.1完整书写形式
3.2继承条件
1.子类继承父类 只能继承一个类(单继承)
2.一个父类可以有多个子类
class Person1{
constructor(name:string,sex:string){
this.name=name
this.sex=sex
}
name:string
sex:string
eat(){
console.log('人的吃喝拉撒');
}
}
class GoodPerson extends Person1{
color:string
sex:string
constructor(name:string,sex:string,color:string){
super(name,sex)
this.color=color
this.sex=sex
}
hs(){
console.log(this.sex+'好人在做好事');
}
eat(){
//调用父类的方法需要使用super关键字
super.eat()
console.log('好人的吃喝拉撒');
}
}
let hr:GoodPerson=new GoodPerson('张三','男','黄')
hr.hs()
hr.eat()
3.object 类是所有类的父类,没有显示继承的类都默认继承Object
4.子类必须调用父类的构造函数,构造函数必须在第一行
constructor(a1:number,a2:number,a3:number, b1:number,b2:number){
//super 作用 调用父类的构造函数 必须写在构造函数的第一行
super(a1,a2,a3)
this.b1=b1
this.b2=b2
}
5.子类不能继承父类的私有属性和方法
6.在子类中调用父类的属性和方法需要使用this关键字
7.子类属性和父类属性同名,默认使用子类的属性
8.方法可以同名, 默认调用子类方法,可以使用super调用父类方法
四.多态
4.1方法的重载
在同一类中,方法名相同,返回值和参数不同 ts中无法使用 arkts中使用
4.2方法的重写
子类重写父类的方法,方法名相同,参数相同 返回值相同或者是其子类
4.3多态书写
instanceof :判断一个对象是不是某个类型
//宠物
class Pet{
//属性
name:string
sex:string
health:number
love:number
constructor( name:string,sex:string, health:number,love:number){
this.name=name
this.sex=sex
this.health=health
this.love=love
}
//方法
eat(food:string){
console.log('宠物在吃'+food+',健康值加1');
this.health+=1;//健康值加1
}
show():string{
return`昵称:${this.name},性别:${this.sex},健康值:${this.health},亲密度:${this.love}`
}
}
//狗
class Dog extends Pet{
type:string
constructor(type:string,name:string,sex:string, health:number,love:number){
super(name,sex,health,love)
this.type=type
}
//重写
eat(food: string): void {
if(this.health<=97){
this.health+=3
}else{
this.health=100
}
console.log(`狗在吃:${food},健康值+3,当前健康值:${this.health}`);
}
show(): string {
let str:string=super.show();
return str+`,品种:${this.type}`
}
jfp(){
console.log(`狗在接飞盘,健康值-10,亲密度+5`);
this.health-=10
this.love+=5
}
}
//猫
class Cat extends Pet{
color:string
constructor(name:string,sex:string, health:number,love:number,color:string){
super(name,sex,health,love)
this.color=color
}
eat(food: string): void {
if(this.health<=90){
this.health+=2
}else{
this.health=100
}
console.log(`猫在吃:${food},健康值+2,当前健康值:${this.health}`);
}
show(): string {
let str:string=super.show();
return str+`颜色:${this.color}`
}
wmx(){
console.log(`猫在玩毛线,健康值-5,亲密度+8`);
this.health-=5
this.love+=8
}
}
//企鹅
class Pengun extends Pet{
age:number
constructor(age:number,name:string,sex:string, health:number,love:number){
super(name,sex,health,love)
this.age=age
}
eat(food: string): void {
if(this.health<=95){
this.health+=5
}else{
this.health=100
}
console.log(`企鹅在吃:${food},健康值+5,当前健康值:${this.health}`);
}
show(): string {
let str:string=super.show();
return str+`,年龄:${this.age}`
}
yy(){
console.log(`企鹅在游泳,健康值-8,亲密度+3`);
this.health-=8
this.love+=3
}
}
//主人 : 领养宠物 喂宠物
class Master{
// getDog():Dog{
// let dog:Dog=new Dog('边牧','阿豪','公',90,80)
//console.log(`恭喜你领养狗狗回家:${dog}`);
// return dog
// }
// getPengun():Pengun{
// let p:Pengun=new Pengun(3,'小边','公',90,80)
// console.log(`恭喜你领养企鹅宝宝回家:${p}`);
// return p
// }
getPet(p:Pet){
console.log(p.show());
}
//以父类作为形参实现多态
//喂宠物
feedPet(p:Pet){
//判断p是不是狗 判断一个对象是不是某个类型
if(p instanceof Dog){
p.eat('骨头')
}else if(p instanceof Pengun){
p.eat('鱼')
}else if (p instanceof Cat){
p.eat('猫粮')
}
}
//玩游戏
play(p:Pet){
if(p instanceof Dog){
//前面已经做了类型判断,所以系统会自动把对象转换为对应的类型
p.jfp()
}else if(p instanceof Pengun){
p.yy()
}else if(p instanceof Cat){
p.wmx()
}
}
}
let m:Master=new Master()
let dd:Dog=new Dog('博美','妹妹','母',80,70)
let str=m.getPet(dd)
//m.feedPet(dd)
let pg:Pengun=new Pengun(2,'琪琪','母',80,70)
//m.feedPet(pg)
//m.play(pg)
let mm:Cat=new Cat('猫猫','白色',90,80,'母')
m.play(mm)
m.feedPet(mm)
五.利用DevEcostudio编写一个虚拟宠物程序实例
5.1页面效果:
首次进入页面
选择宠物页面
5.2创建方式
在ets文件夹中常见Model文件夹,把上面写的ts文件放入里面
4.3 狗.TS
//狗
import { Pet } from './Pet'
import { promptAction } from '@kit.ArkUI'
export class Dog extends Pet{
type:string
constructor(type:string,name:string,sex:string, health:number,love:number){
super(name,sex,health,love)
this.type=type
}
//重写
eat(food: string): void {
if(this.health<=97){
this.health+=3
}else{
this.health=100
}
console.log(`狗在吃:${food},健康值+3,当前健康值:${this.health}`);
}
jfp(){
console.log(`狗在接飞盘,健康值-10,亲密度+5`);
this.health-=10
if(this.health<=0){
this.love=0
promptAction.showToast({message:'健康值太低,您的宠物已死亡'})
}else if(this.health<=40){
this.love-=10
promptAction.showToast({message:'宠物健康值较低,小心死亡'})
}
if(this.love<=95){
this.love+=5
}else{
this.love=100
}
}
}
4.4主人.TS
//主人 : 领养宠物 喂宠物
import { Cat } from './Cat';
import { Dog } from './Dog';
import { Pengun } from './Pengun';
import { Pet } from './Pet';
//主人 领养宠物
export class Master{
getPet(p:Pet){
}
//以父类作为形参实现多态
//喂宠物
feedPet(p:Pet){
//判断p是不是狗 判断一个对象是不是某个类型
if(p instanceof Dog){
p.eat('骨头')
}else if(p instanceof Pengun){
p.eat('鱼')
}else if(p instanceof Cat){
p.eat('猫粮')
}
}
//玩游戏
play(p:Pet){
if(p instanceof Dog){
//前面已经做了类型判断,所以系统会自动把对象转换为对应的类型
p.jfp()
}else if(p instanceof Pengun){
p.yy()
}else if(p instanceof Cat){
p.wmx()
}
}
}
4.5 企鹅.TS
//企鹅
import { Pet } from './Pet'
import { promptAction } from '@kit.ArkUI'
export class Pengun extends Pet{
age:number
constructor(age:number,name:string,sex:string, health:number,love:number){
super(name,sex,health,love)
this.age=age
}
eat(food: string): void {
if(this.health<=95){
this.health+=5
}else{
this.health=100
}
console.log(`企鹅在吃:${food},健康值+5,当前健康值:${this.health}`);
}
yy(){
console.log(`企鹅在游泳,健康值-8,亲密度+3`);
this.health-=8
if(this.health<=0){
this.love=0
promptAction.showToast({message:'健康值太低,已死亡'})
}else if(this.health<=40){
this.love-=8
promptAction.showToast({message:'宠物健康值较低,小心死亡'})
}
if(this.love<=97){
this.love+=3
}else{
this.love=100
}
}
}
4.6宠物.TS
//宠物
export class Pet{
//属性
name:string
sex:string
health:number
love:number
constructor( name:string,sex:string, health:number,love:number){
this.name=name
this.sex=sex
this.health=health
this.love=love
}
//方法
eat(food:string){
console.log('宠物在吃'+food+',健康值加1');
this.health+=1;//健康值加1
}
}
4.7猫.TS
//猫
import { Pet } from './Pet'
import { promptAction } from '@kit.ArkUI'
export class Cat extends Pet{
color:string
constructor(name:string,sex:string, health:number,love:number,color:string){
super(name,sex,health,love)
this.color=color
}
eat(food: string): void {
if(this.health<=90){
this.health+=2
}else{
this.health=100
}
console.log(`猫在吃:${food},健康值+2,当前健康值:${this.health}`);
}
wmx(){
console.log(`猫在玩毛线,健康值-5,亲密度+8`);
this.health-=5
if(this.health<=0){
this.love=0
promptAction.showToast({message:'健康值太低,已死亡'})
}else if(this.health<=40){
this.love-=10
promptAction.showToast({message:'您的宠物健康值较低,小心死亡'})
}
if (this.love<=97) {
this.love+=3
}else{
this.love=100
}
}
}
六.在主页面创建实例化对象
6.1变量的声明
ResourceStr 资源字符串
@State message: string = '虚拟宠物';
@State ck:number=-1;//-1未选择,0为企鹅 1,狗;2,猫 单选的变量
//ResourceStr 资源字符串
@State img:ResourceStr=$rawfile('2024-08-28_144259.png')
@State pet:Pet=new Pet('','',80,80)
@State dog:Dog=new Dog('','','',0,0)
@State pg:Pengun=new Pengun(0,'','',0,0)
@State cat:Cat=new Cat('','',0,0,'')
@State flag:boolean=false
创建单选按钮,选中哪个按钮,把里面的值改为相对应的数字,0代表企鹅,1狗 2.猫 -1未选择 -2 确认页面
如果为领养宠物,则没有输入框,如果选择宠物则出现输入框,输入宠物的姓名等属性
确认按钮:如果没有输入属性值则提示不能为空,健康值和亲密值,年龄不能为非数字
符合要求则领养成功进入喂养页面和玩耍页面 如果健康值小于0宠物则死亡重新进入领养页面,如果亲密度小于0则离家出走
猫,健康值小于40亲密度-10 玩耍健康值-8, 亲密度+5,吃食物:健康值+5
狗:健康值小于40亲密度-10 玩耍健康值-10,亲密度+5,吃食物:健康值+3
企鹅:健康值小于40亲密度-8 玩耍健康值-8,亲密度+3,吃食物:健康值+5