鱼C论坛

 找回密码
 立即注册
查看: 3141|回复: 2

[学习笔记] JAVA学习Day10【面对对象4】

[复制链接]
发表于 2020-8-19 19:35:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学习视频:https://www.bilibili.com/video/BV17J411G72L?p=20

178-195



  1. /*
  2. *类的成员:
  3. *(1)属性,成员变量
  4. *(2)方法,成员方法
  5. *(3)构造器
  6. *(4)代码块:非静态代码块,静态代码块
  7. *
  8. *
  9. */

  10. /*
  11. *非静态代码块中的代码在什么时候执行?
  12. *每次在创建对象的时候都执行,且比构造器早
  13. *
  14. *实例初始化过程,创建对象时,为对象进行初始化的操作:
  15. *(1)为成员变量显示赋值
  16. *(2)执行非静态代码块
  17. *(3)执行构造器
  18. *Java编译器其实会把这三个部分的代码合成一个叫做<init>(【形参列表】)实力初始化方法
  19. *即编译后的class字节码信息中,是没有构造器这个概念
  20. *<init>(【形参列表】)实力初始化方法的代码就是由三个部分组成
  21. *(1)成员变量显示赋值的代码
  22. *(2)非静态代码块中的代码
  23. *(3)执行构造器中的代码
  24. *(1)(2)按顺序执行,(3)一定是它们当中的最后执行
  25. *
  26. *而且,有几个构造器,就会有几个实例初始化的方法,那么导尿管你创建对象的时候,调用对应的构造器时,其实执行的是对应的实例初始化方法<init>(【...】)
  27. *
  28. */

  29. public class Test10_01{
  30.         public static void main(String[] args){
  31.                 MyClass my1 = new MyClass();//调用无参构造
  32.                 MyClass my2 = new MyClass("atguigu");//调用有参构造

  33.                 Demo d1 = new Demo();
  34.                 Demo d2 = new Demo("atguigu");
  35.         }
  36. }

  37. class MyClass{
  38.         private String str = "hello";//显示赋值

  39.         public MyClass(){
  40.                 System.out.println("无参构造");
  41.         }
  42.         public MyClass(String str){
  43.                 this.str = str;
  44.                 System.out.println("有参构造");
  45.         }

  46.         {
  47.                 System.out.println("非静态代码块");
  48.         }
  49. }


  50. class Demo{
  51.         private String str = assign();

  52.         public Demo(){
  53.                 System.out.println("无参构造");
  54.         }
  55.         public Demo(String str){
  56.                 this.str = str;
  57.                 System.out.println("有参构造");
  58.         }
  59.         {
  60.                 System.out.println("非静态代码块");
  61.         }
  62.         public assign(){
  63.                 System.out.println("assign方法");
  64.                 return "hello";
  65.         }
  66. }


  67. /*
  68. *实例初始化示例(1)看一下运行结果并解释!!!!!!!!!!!!
  69. *
  70. */
  71. public class Test10_02{
  72.         public static void main(String[] args){
  73.                 //Father f = new Father();

  74.                 //Son s = new Son();

  75.                 //Son s2 = new Son("atguigu");

  76.                 Son s3 = new Son("atguigu", 10);
  77.         }
  78. }


  79. class Father{
  80.         public Father(){
  81.                 System.out.println("父类的无参构造");
  82.         }
  83. }

  84. class Son extends Father{
  85.         private String str;

  86.         public Son(){
  87.                 //隐含了super();子类构造器中一定会调用父类的构造器,默认调用父类的无参构造
  88.                 System.out.println("子类的无参构造");
  89.         }

  90.         public Son(String str){
  91.                 //隐含了super();子类构造器中一定会调用父类的构造器,默认调用父类的
  92.                 this(str);
  93.                 this.num = num;
  94.                 System.out.println("子类中的有参构造");
  95.         }
  96. }


  97. /*(1)先执行父类的实例初始化方法
  98. *它由三部分组成
  99. *1.成员变量的显式赋值 2.非静态代码块 3.构造器
  100. *(2)再执行子类的实例初始化方法
  101. *1.成员变量的显式赋值 2.非静态代码块 3.构造器
  102. *
  103. *super()或super(实参列表)之前说的是调用父类的构造器,其实是调用父类对应的实例初始化方法
  104. *super()或super(实参列表)之前说的是在子类构造器的首行,其实是在子类实例初始化方法的首行
  105. *
  106. */
  107. public class Test10_03{
  108.         public static void main(String[] args){
  109.                 Zi z = new Zi();
  110.         }
  111. }

  112. class Fu{
  113.         private String str = assign();
  114.         {
  115.                 System.out.println("(1)父类的非静态代码块");
  116.         }
  117.         public Fu(){
  118.                 System.out.println("(2)父类的无参构造")
  119.         }
  120.         public String assign(){
  121.                 System.out.println("(3)父类的assign()");
  122.                 return "fu";
  123.         }
  124. }

  125. class Zi extends Fu{
  126.         private String strZi = assignZi();
  127.         {
  128.                 System.out.println("(4)子类的非静态代码块");
  129.         }
  130.         public Zi(){
  131.                 //super(); ==> 调用父类的实例初始化方法,而且它在子类实例初始化方法的首行
  132.                 System.out.println("(5)子类的无参构造");
  133.         }
  134.         public String assignZi(){
  135.                 System.out.println("(6)子类的assignZi()");
  136.                 return "zi";
  137.         }
  138. }



  139. /*
  140. *面向对象的基本特征:1.封装 2.继承 3.多态
  141. *多态:多种形态
  142. *变量的引用形式:
  143. *(1)本态引用:左边的变量与右边的对象是同一种类型
  144. *(2)多态引用,左边的变量是父类类型,右边的对象是子类的对象
  145. *
  146. *多态的表现特征:编译类型与运行时的类型不一致
  147. *编译的时候,按照父类类型编译的
  148. *执行的方法是子类重写的方法
  149. *编译看左边,运行看右边
  150. *
  151. *多态的前提:(1)继承(2)重写(3)多态引用
  152. *
  153. *用途:方法的动态绑定
  154. *多态和属性无关,只和方法有关
  155. */

  156. public class Test10_04{
  157.         public static void main(String[] args){
  158.                 //1.本态引用
  159.                 Person p = new Person();
  160.                 Woman w = new Woman();
  161.                 Man m = new Man();

  162.                 //2.多态引用
  163.                 Person p2 = new Woman();
  164.                 Person p3 = new Man();

  165.                 p2.eat();
  166.                 p2.walk();
  167.                 p2.shop();//不能调用
  168.         }
  169. }


  170. class Person{
  171.         public void eat(){
  172.                 System.out.println("吃饭");
  173.         }
  174.         public void walk(){
  175.                 System.out.println("走路");
  176.         }
  177. }

  178. class Woman extends Person{
  179.         public void eat(){
  180.                 System.out.println("细嚼慢咽");
  181.         }
  182.         public void walk(){
  183.                 System.out.println("婀娜");
  184.         }
  185.         public void shop(){
  186.                 System.out.println("买买买");
  187.         }
  188.        
  189. }

  190. class Man extends Person{
  191.         public void eat(){
  192.                 System.out.println("狼吞虎咽");
  193.         }
  194.         public void walk(){
  195.                 System.out.println("大摇大摆");
  196.         }
  197.         public void smoke(){
  198.                 System.out.println("吐烟");
  199.         }
  200. }


  201. /*
  202. *多态的应用
  203. *(1)多态数组
  204. *
  205. */

  206. public class test10_05{
  207.         public static void main(String[] args){
  208.                 //创建一个数组,可以存储各种图的对象,包括圆、矩形等等
  209.                 Graphic[] all = new Graphic[3];
  210.                 all[0] = new Circle(1.3);
  211.                 all[1] = new Rectangle(2,4);

  212.                 for(int i = 0; i < all.length; i++){
  213.                         System.out.println("面积:" + all[i].getArea());
  214.                 }
  215.         }
  216.        
  217. }


  218. class Graphic{
  219.         public double getArea(){
  220.                 return 0.0;
  221.         }
  222. }

  223. class Circle extends Graphic{
  224.         private double radius;

  225.         public Circle(double radius){
  226.                 this.radius = radius;
  227.         }

  228.         public double getArea(){
  229.                 return 3.14 * radius * radius;
  230.         }
  231. }

  232. class Rectangle extends Graphic{
  233.         private double length;
  234.         private double width;

  235.         public Rectangle(double length, double width){
  236.                 this.length = length;
  237.                 this.width = width;
  238.         }
  239.         //重写
  240.         public double getArea(){
  241.                 return length * width;
  242.         }
  243. }


  244. /*
  245. *多态的应用
  246. *(2)多态参数
  247. *
  248. */

  249. public class test10_06{
  250.         //检查所有动物吃东西是否正常
  251.         public static void check(Animal a){
  252.                 a.eat();

  253.         }
  254.         public static void main(String[] args){
  255.                 //匿名对象
  256.                 check(new Dog());
  257.                 check(new Cat());

  258.                 Dog d = new Dog();
  259.                 check(d);
  260.         }
  261. }


  262. class Animal{
  263.         public void eat(){
  264.                 System.out.println("吃东西");
  265.         }
  266.                
  267. }

  268. class Dog extends Animal{
  269.         public void eat(){
  270.                 System.out.println("啃骨头");
  271.         }
  272. }

  273. class Cat extends Animal{
  274.         public void eat(){
  275.                         System.out.println("吃鱼");
  276.         }
  277.        
  278. }


  279. /*
  280. *类型转换:向上转型与向下转型
  281. *1.基本数据类型的转换
  282. (1)自动类型转换
  283. *byte -> short -> int -> long -> float -> double
  284. *   char ->
  285. *(2)强制类型转换
  286. *double -> float -> long -> int -> short -> byte
  287. *  ->char
  288. *2.引用数据类型数据
  289. *父子类之间的转换(不是父子类之间是不能进行向上与向下转型的)
  290. *(1)向上转型 从子类到父类
  291. *(2)向下转型 从父类到子类
  292. *为什么要向上转型?
  293. *因为多态数组、多态参数的应用场景,使得有时候不得不向上转型,这样是为了方便统一管理各种子类对象
  294. *为什么要向下转型?为了调用子类特有的方法
  295. *是否所有的父类向下转型都会成功呢?
  296. *要想转型成功,之前必须要向上转型,才能向下转型
  297. *
  298. *
  299. *异常总结:
  300. *(1)ArrayIndexOutOfBoundsException:数组下标越界异常
  301. *(2)NullPointerException:空指针异常
  302. *对象.属性
  303. *对象.方法
  304. *如果对象是null,就会发生空指针异常
  305. *(3)ClassCastException:类型转换异常
  306. *向下转型时,可能会发生
  307. *
  308. */

  309. public class Test10_07{
  310.         public static void main(String[] args){
  311.                 //(1)向上转型
  312.                 Person p = new Woman();//多态引用
  313.                 //一旦把Woman对象向上转型为Person后,就只能调用父类的方法
  314.                 p.eat();
  315.                 p.walk();

  316.                 //(2)向下转型
  317.                 //在这里调用Woman特有的方法
  318.                 Woman m = (Woman)p;
  319.                 m.shop();

  320.                 Person p2 = new Person();
  321.                 Woman w2 = (Woman) p2;//类型转换错误
  322.                 w2.shop();

  323.                 Person p3 = new Man();
  324.                 Woman w3 = (Woman) p3;//类型转换错误
  325.                 w3.shop();


  326.         }
  327. }


  328. class Person{
  329.         public void eat(){
  330.                 System.out.println("吃饭");
  331.         }
  332.         public void walk(){
  333.                 System.out.println("走路");
  334.         }
  335. }

  336. class Woman extends Person{
  337.         public void eat(){
  338.                 System.out.println("细嚼慢咽");
  339.         }
  340.         public void walk(){
  341.                 System.out.println("婀娜");
  342.         }
  343.         public void shop(){
  344.                 System.out.println("买买买");
  345.         }
  346.        
  347. }

  348. class Man extends Person{
  349.         public void eat(){
  350.                 System.out.println("狼吞虎咽");
  351.         }
  352.         public void walk(){
  353.                 System.out.println("大摇大摆");
  354.         }
  355.         public void smoke(){
  356.                 System.out.println("吐烟");
  357.         }
  358. }



  359. /*
  360. *向下转型时会有风险,可能会发生ClassCastException
  361. *因为这是个运行异常,不能提前发现
  362. *使用代码避免,用instanceof
  363. *
  364. *什么情况下用instanceof判断返回true?
  365. *if(p instanceof Woman) 当p中存储的就是Woman的对象
  366. *if(p instanceof Woman) 当p中存储的是Woman的子类对象
  367. */


  368. public class Test10_08{
  369.         public static void main(String[] args){

  370.         }
  371.         public static void test(Person p){
  372.                 //如果这个p是个女人,就调用它的shop方法
  373.                 //如果这个p是个男人,就调用它的smoke方法
  374.                 //如果这样写会出问题,因为Person包含了Woman和Man
  375.                 /*
  376.                 *if(p instanceof Person){
  377.                 *        p.eat();
  378.                 *        p.walk();
  379.                 *}else*/if(p instanceof Woman){
  380.                         Woman m = (Woman) p;
  381.                         m.shop();
  382.                 }else if(p instanceof Man){
  383.                         Man m = (Man) p;
  384.                         m.smoke();
  385.                 }
  386.                
  387.         }
  388. }

复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2020-8-22 19:38:08 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-2 09:58:12 | 显示全部楼层
还是通俗易懂得
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 11:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表