侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

java封装继承多态等

Java  /  管理员 发布于 3年前   284

       最近一段时间看了很多的视频却忘记总结了,现在只能想到什么写什么了,希望能起到一个回忆巩固的作用。

    1、final关键字

      译为:最终的

         final修饰的数据类型的量都是不能改变的

        final修饰的类是不能被继承的

        final修饰的方法在子类中是不能被重写的

package cn.bjsxt.oop.testFinal;public /*final*/ class Animal {    //final修饰类则说明,这个类不能被继承!public /*final*/ void run(){   //final加到方法前面,意味着该方法不能被子类重写!System.out.println("跑跑!");}}class Bird  extends Animal {public void run(){super.run();System.out.println("我是一个小小小小鸟,飞呀飞不高");}}package cn.bjsxt.oop.testFinal;public class TestFinal {public static void main(String[] args) {final int MAX_VALUE= 200;    //常量。double d = Math.PI;}}

 

 

2、隐式参数this

java类里面的方法都有两个隐藏的参数分别是super和this,分别表示直接父类和自己类的对象。默认就加入进去了,不需要手动添加

 

package cn.bjsxt.oop.testThis;public class Student { String name; int id;     public  Student(String name,int id){ this(name);   //通过this调用其他构造方法,必须位于第一句! Constructor call must be the first statement in a constructor this.name = name; this.id = id; } //本来是这样 public Student(String name,Student this,Object super){ this.name = name; } public Student(){ System.out.println("构造一个对象"); }  public void setName(String name){ this.name = name; }   public void study(){ this.name=  "张三"; System.out.println(name+"在學習"); }  public void sayHello(String sname){ System.out.println(name+"向"+sname+"說:你好!"); }}

 3、Object类,Object是所有类的父类,默认就继承这个类

 

package cn.bjsxt.oop.testObject;//不重写Object类的toString方法时,我们打印对象会直接输出hash码,重写之后就返回这个字符串public class Mobile {public String toString(){return "我是一部移动电话";}}package cn.bjsxt.oop.testObject;public class TestObject {public static void main(String[] args) {Object obj = new Object();Object obj2 = new Object();System.out.println(obj.toString());System.out.println(obj2.toString());System.out.println(obj==obj2);System.out.println(obj.equals(obj2)); Mobile m = new Mobile();System.out.println(m.toString()); }}

 输出:java.lang.Object@64ea66
java.lang.Object@158f9d3
false
false

我是一部移动电话

 

静态

 

package cn.bjsxt.oop.testStatic;public class Student { String name; int id;     static  int  ss;  public static void printSS(){ System.out.println(ss); }  public void study(){ printSS(); System.out.println(name+"在學習"); }  public void sayHello(String sname){ System.out.println(name+"向"+sname+"說:你好!"); }}package cn.bjsxt.oop.testStatic;public class Test {public static void main(String[] args) {Student.ss = 323;Student.printSS();Student s1 = new Student();}}

 

静态代码块

package cn.bjsxt.oop.staticInitBlock;public class Parent001 /*extends Object*/ {static int aa;static {System.out.println(" 静态初始化Parent001");aa=200;}}package cn.bjsxt.oop.staticInitBlock;public class TestStaticInitBlock extends Parent001 {static int a ;static {System.out.println("静态初始化TestStaticInitBlock!");a = 100;}public static void main(String[] args) {}}

  静态初始化Parent001
静态初始化TestStaticInitBlock!

 

封装encapsulation,记忆capsul,胶囊

比如把属性设为私有,同时提供共有的方法访问或者设置属性值

 

继承与组合

继承:isa

package cn.bjsxt.oop.inherit;/** * 测试继承 * @author dell * */public class Animal /*extends Object*/ {String eye;public void run(){System.out.println("跑跑!");}public void eat(){System.out.println("吃吃!");}public void sleep(){System.out.println("zzzzz");}public  Animal(){super();System.out.println("创建一个动物!");}}class Mammal extends Animal {public void taisheng(){System.out.println("我是胎生");}}class Bird  extends Animal {public void run(){super.run();System.out.println("我是一个小小小小鸟,飞呀飞不高");}public void eggSheng(){System.out.println("卵生");}public Bird(){super();System.out.println("建一个鸟对象");}}

 

 

组合:hasa

package cn.bjsxt.oop.inherit;/** * 测试组合 * @author dell * */public class Animal2 {String eye;public void run(){System.out.println("跑跑!");}public void eat(){System.out.println("吃吃!");}public void sleep(){System.out.println("zzzzz");}public  Animal2(){super();System.out.println("创建一个动物!");}public static void main(String[] args) {Bird2 b = new Bird2();b.run();b.animal2.eat();}}class Mammal2  {Animal2 animal2=new Animal2();public void taisheng(){System.out.println("我是胎生");}}class Bird2  {Animal2 animal2=new Animal2();public void run(){animal2.run();System.out.println("我是一个小小小小鸟,飞呀飞不高");}public void eggSheng(){System.out.println("卵生");}public Bird2(){super();System.out.println("建一个鸟对象");}}

 

 

继承时带来的强制类型转换,和基本数据类型一样,引用数据类型也会有类型的转化

package cn.bjsxt.oop.polymorphism;public class Animal {String str;public void voice(){System.out.println("普通动物叫声!");}}class Cat extends Animal {public void voice(){System.out.println("喵喵喵");}public void catchMouse(){System.out.println("抓老鼠");}}class Dog extends Animal {public void voice(){System.out.println("汪汪汪");}public void seeDoor(){System.out.println("看门!");}}class Tiger extends Animal {public void voice(){System.out.println("哇哇哇");}}class Pig extends Animal {public void voice(){System.out.println("哼哼哼");}}

 动物都会发声,重写voice方法,不同的动物有不同的发声方法。有的动物还有自己父类所不具备的方法,猫会抓老鼠,狗会看门。

 

package cn.itcast_04;public class Test {public static void testAnimalVoice(Animal c){c.voice();if(c instanceof Cat){((Cat) c).catchMouse();}} public static void testAnimalVoice(Dog c){c.voice();}public static void testAnimalVoice(Pig c){c.voice();}  public static void main(String[] args) {//父类的引用指向子类的对象              Animal a = new Cat();                //强制类型转化,低到高Cat a2 = (Cat)a;                //a是Cat实例的引用,会调用捉老鼠的方法testAnimalVoice(a);                / /完成了强制类型转化              a2.catchMouse(); Animal b = new Dog(); Animal c = new Tiger(); testAnimalVoice(b); testAnimalVoice(c);}}

 要考虑编译(eclipse不带有红叉报错)和运行阶段的错误(不抛出异常,比如ClassCastException)

代码改成:

 Animal a = new Animal();
                //强制类型转化,低到高
Cat a2 = (Cat)a;
则:抛出

cn.itcast_04.Animal cannot be cast to cn.itcast_04.Cat。

父类不可以强制转换为子类,因为子类有的方法父类可能没有

 

 

 



  • 上一条:
    PHP大数据导出之扩展phpoffice/phpexcel跟rap2hpoutre/fast-excel的测试效果对比
    下一条:
    Java序列化进阶篇
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • java正则表达式彻底研究(0个评论)
    • java正则表达式验证函数(0个评论)
    • MVC、MVP和MVVM分别是什么(0个评论)
    • java 单例模式(饿汉模式与懒汉模式)(0个评论)
    • 近期文章
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • 在go语言中用filepath.Match()函数以通配符模式匹配字符串示例(0个评论)
    • Laravel Response Classes 响应类使用优化浅析(0个评论)
    • mysql中sql_mode的各模式浅析(0个评论)
    • 百度文心一言今天发布,个人第一批内测体验记录,不好别打我(0个评论)
    • 嘿加密世界让我们谈谈在共识中将中本聪主流化(0个评论)
    • 在go语言中寻找两个切片或数组中的相同元素/共同点/交集并集示例代码(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-11
    • 2018-03
    • 2020-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客