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

科室网站建设有了域名之后如何做网站

科室网站建设,有了域名之后如何做网站,进入公众号继续阅读下一章,怎么免费开网站目录 一.向上转型 直接赋值 总结: 通过传参 通过返回值 二.向下转型 instanceof 一.向上转型 向上转型其实就是创建一个子类对象,并将其当作父类对象来使用,一般语法格式如下: 父类类型 对象名 new 子类类型() 一般有以…

目录

一.向上转型

直接赋值 

总结:

通过传参

通过返回值

二.向下转型

instanceof 


一.向上转型

向上转型其实就是创建一个子类对象,并将其当作父类对象来使用,一般语法格式如下:

父类类型 对象名 = new 子类类型()

一般有以下三种使用场景:

直接赋值 

我们这里以父类为Animal类举例,并且有一个Cat类来继承他 

class Animal {String name;int age;//构造方法public Animal(String name,int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name + "正在吃食物~~~");}
}
class Cat extends Animal {//构造方法public Cat(String name,int age) {super(name,age);}
}

        在这种情况下,我们用父类对象直接引用子类对象,不但程序不会报错,反而还可以正常调用父类中的成员方法

    public static void main(String[] args) {Cat cat = new Cat("布偶",3);Animal animal1 = cat;animal1.eat();Animal animal2 = new Cat("橘猫",2);animal2.eat();}

输出结果: 

那能不能调用子类的成员方法呢?我们在子类中新添加一个sleep方法

class Animal {String name;int age;//构造方法public Animal(String name,int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name + "正在吃食物~~~");}
}
class Cat extends Animal {//构造方法public Cat(String name,int age) {super(name,age);}//新添加的子类方法public void sleep() {System.out.println(this.name + "正在睡觉......");}
}

我们尝试用上述过程来调用以下子类的中的方法:

    public static void main(String[] args) {       Animal animal = new Cat("橘猫",2);animal.eat();animal.sleep();   }

但是等我们写好了后,就会发现编译器报错了 

也就是说:

使用向上转型后,我们可以正常调用父类中的成员方法,但是不能调用子类中自己的方法

但是要解决上述问题还是很简单的,当我们在子类中重写父类方法后,我们就可以调用子类方法了,这也就是我们多态的实现

class Animal {String name;int age;//构造方法public Animal(String name,int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name + "正在吃食物~~~");}public void sleep() {}
}
class Cat extends Animal {//构造方法public Cat(String name,int age) {super(name,age);}public void sleep() {System.out.println(this.name + "正在睡觉......");}
}
    public static void main(String[] args) {Animal animal = new Cat("橘猫",2);animal.eat();animal.sleep();}

现在我们就可以正常调用输出了:

总结:

当我们通过向上转型新建了一个对象后,我们可以通过这个对象来访问父类中的方法,但是不能访问子类中自己的方法,除非我们在子类中重写父类方法,也就是说当一个方法只有在父类和子类中都出现的情况下,我们才能访问子类中的这个方法

通过向上转型新建的对象可以访问的方法范围是:

  • 父类有 子类没有
  • 父类有 子类也有 

不能访问的范围是

  • 父类没有 子类也没有
  • 父类没有 但子类有 

通过传参

class Animal {String name;int age;//构造方法public Animal(String name,int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name + "正在吃食物~~~");}
}
class Dog extends Animal {//构造方法public Dog(String name,int age) {super(name,age);}public void eat(){System.out.println(this.name + "正在吃狗粮~~~");}
}
class Cat extends Animal {//构造方法public Cat(String name,int age) {super(name,age);}public void eat(){System.out.println(this.name + "正在吃猫粮~~~");}
}
    public static void fun(Animal animal) {animal.eat();}
fun(new Dog("哈士奇",2));
fun(new Cat("橘猫",2));

 我们通过一个方法来接受一个父类对象,然后通过这个父类对象来调用eat方法,当我们传入的参数是一个子类对象的时候,就相当于我们用父类对象做参数来接到了子类对象,也就是发生了向上转型

输出结果:

通过返回值

class Animal {String name;int age;//构造方法public Animal(String name,int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name + "正在吃食物~~~");}
}
class Dog extends Animal {//构造方法public Dog(String name,int age) {super(name,age);}public void eat(){System.out.println(this.name + "正在吃狗粮~~~");}
}
class Cat extends Animal {//构造方法public Cat(String name,int age) {super(name,age);}public void eat(){System.out.println(this.name + "正在吃猫粮~~~");}
}
    public static Animal fun1(Animal animal) {return new Cat("布偶",3);}public static Animal fun2(Animal animal) {return new Dog("哈士奇",2);}

我们用父类对象做返回值类型,但是我们返回的是子类对象,这也就相当于发生了向上转型

向上转型的优点:让代码实现更简单灵活。
向上转型的缺陷:不能调用到子类特有的方法 

二.向下转型

将一个子类对象经过向上转型之后当成父类方法使用,再无法调用子类的方法,但有时候可能需要调用子类特有的方法,此时:将父类引用再还原为子类对象即可,即向下转换

 

class Animal {String name;int age;//构造方法public Animal(String name,int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name + "正在吃食物~~~");}public void sleep() {}
}
class Cat extends Animal {//构造方法public Cat(String name,int age) {super(name,age);}public void sleep() {System.out.println(this.name + "正在睡觉......");}
}
public class Test_4 {public static void main(String[] args) {Animal animal = new Cat("橘猫",2);Cat cat = new Cat("布偶",2);animal.eat();animal.sleep();cat = (Cat)animal;cat.eat();cat.sleep();}
}

 输出结果:

instanceof 

向下转型用的比较少,而且不安全,万一转换失败,运行时就会抛异常。Java中为了提高向下转型的安全性,引入了 instanceof  ,如果该表达式为true,则可以安全转换 

public static void main(String[] args) {Cat cat = new Cat("元宝",2);Dog dog = new Dog("小七", 1);// 向上转型Animal animal = cat;animal.eat();animal = dog;animal.eat();if(animal instanceof Cat) {cat = (Cat)animal;}if(animal instanceof Dog) {dog = (Dog)animal;}
}

对于instanceof的详细信息可以查看官方资料:

 Chapter 15. Expressions (oracle.com)

 




http://www.yayakq.cn/news/135432/

相关文章:

  • 口碑好网站建设公司电话网站重新建设的通知
  • 网站开发算互联网公司吗优化网站做什么的
  • 关于幼儿建设网站ppt模板下载wordpress对虚拟主机的要求
  • 女装网站建设规划书做外贸网站包括哪些
  • 网站建设哪家好 需要多少钱wordpress动画轮播代码教程
  • 什么是电子商务系统信息流优化师是干什么的
  • 简单的英文网站模板哪里有焊接加工外发的
  • 满城建设局官方网站wordpress博客网站
  • 广西哪里有网站建设中国建筑装饰集团有限公司
  • 网站管理公司 优帮云导航站 wordpress
  • 海淘网站主要关键词wordpress调起淘宝app
  • 在对方网站做友情链接有限公司和股份有限公司的区别
  • 网站开发用户名不存在手机端的网页
  • 南京网站设计公司兴田德润放心在百度做推广送网站好吗
  • 泰安市建设职工培训中心网站进不去广州网站建设 易点
  • 网站备案号怎么申请辽宁省建设厅投诉网站
  • 政网站首页怎么做试中东跨境电商平台有哪些
  • 网站制作与建设与网页制作网络营销出来可以干什么工作
  • 网站建设类论文wordpress升级vip
  • 专做耐克阿迪鞋网站有了域名后怎么做网站
  • 石家庄集团网站建设浦口区网站建站
  • 辽宁做网站和优化百度域名是什么意思
  • 廉洁广州在线网站建设地方网站怎么做挣钱
  • 如何做音乐分享类网站文件外链生成网站
  • 建网站跟建网店的区别怎么做网络推广
  • 做智能网站营销话术手机网站建设价格低
  • 济南网站建设外包公司排名wordpress 取消边栏
  • flask 简易网站开发手机网站 切图
  • 怎么判断网站是否被k企业邮箱如何查询
  • 建设一个收入支出持平的网站聊城建设网站