zoukankan      html  css  js  c++  java
  • 2019-05-24 java学习日记

    面向对象

    代码块的概述与分类

    在java中,使用 { } 括起来的被称为代码块

    代码块分类:

    根据其位置和声明的不同,可以分为局部代码块,构造代码块,静态代码块,同步代码块(多线程讲解)

    常见代码块的应用:

    局部代码块:

      在方法内出现,限定变量的生命周期,及早释放,提高内存利用率

    构造代码块(初始化块):

      在类中方法外出现;多个构造方法中相同的代码存放在一起,
      每次跳用构造都执行,并且在构造方法前执行

    静态代码块:

      1,在类中方法外出现,加了static修饰

      2,在类中方法外出现,并加上static修饰;用于给类进行初始化,在加载的时候执行,并且只执行一次

      3,一般用于加载驱动

    比方法便优先执行,

     1 class Demo1_Code {
     2     public static void main(String[] args) {
     3         {
     4             int x = 10;                        //限定变量的声明周期
     5             System.out.println(x);
     6         }
     7         
     8         Student s1 = new Student();
     9         System.out.println("---------------");
    10         Student s2 = new Student("张三",23);
    11     
    12     }
    13 
    14     static {
    15         System.out.println("我是在主方法类中的静态代码块");
    16     }
    17 }
    18 
    19 class Student {
    20     private String name;
    21     private int age;
    22 
    23     public Student(){
    24         //study();
    25         System.out.println("空参构造");
    26     }                                    //空参构造
    27 
    28     public Student(String name,int age) {//有参构造
    29         //study();
    30         this.name = name;
    31         this.age = age;
    32         System.out.println("有参构造");
    33     }
    34 
    35     public void setName(String name) {
    36         this.name = name;
    37     }
    38 
    39     public String getName() {
    40         return name;
    41     }
    42 
    43     public void setAge(int age) {
    44         this.age = age;
    45     }
    46 
    47     public int getAge() {
    48         return age;
    49     }
    50 
    51     {    //构造代码块:每创建一次对象就会执行一次,优先于构造函数执行
    52         //System.out.println("构造代码块");
    53         study();
    54     }
    55 
    56     public void study() {
    57         System.out.println("学生学习");
    58     }
    59 
    60     static {                                    
    61         System.out.println("我是静态代码块");    
    62                 //随着类加载而加载,且只执行一次
    63                //作用:用来给类进行初始化,一般用来加载驱动
    64               //静态代码块是优先于主方法执行
    65     }
    66 }
    例子
     1 class Student {
     2     static {
     3         System.out.println("Student 静态代码块");
     4     }
     5     
     6     {
     7         System.out.println("Student 构造代码块");
     8     }
     9     
    10     public Student() {
    11         System.out.println("Student 构造方法");
    12     }
    13 }
    14 
    15 class Demo2_Student {
    16     static {
    17         System.out.println("Demo2_Student静态代码块");
    18     }
    19     
    20     public static void main(String[] args) {
    21         System.out.println("我是main方法");
    22         
    23         Student s1 = new Student();
    24         Student s2 = new Student();
    25     }
    26 }
    例子2

    extends(继承)

    让类与类之间产生关系,父子类的关系

     1 class Demo_Animal{
     2     public static void main(String[] args ) {
     3         cat c = new Cat();
     4         c.name = "汽水"
     5         c.color = "桔色"
     6         c.eat();
     7         c.sleep();
     8         c.play();
     9         System.out.println("名字:" + c.name );
    10         System.out.println("颜色:" + c.color );
    11         System.out.println("在干嘛:" + c.play );
    12     }
    13 }
    14 class Animal {
    15         String name;
    16         String color;
    17      public void eat() {
    18        System.out.println("吃饭"); 
    19     }
    20      public void sleep() {
    21        System.out.println("睡觉"); 
    22     }
    23      public void play() {
    24        System.out.println("打豆豆"); 
    25     }
    26 }    
    27 class Cat extends Animal{ }  
    28 class Dog extends Animal{ }    
    29 /*
    30 extends是继承的意思
    31 Animal是父类
    32 Cat和Dog都是子类
    33 */     
    例子

    继承的好处与弊端

    好处:

      提高了代码的复用性

      提高了代码的维护性

      让类与类之间产生了关系,是多态的前提

    弊端:

      类的耦合性增强了

    开发的原则:高内聚,低耦合

      耦合:类与类的关系

      内聚:就是自己的完成某件事情的能力

    java中类的继承特点

    1,Java只支持单继承,不支持多继承。
    2,有些语言是支持多继承,格式:extends 类1,类2,...

    Java支持多层继承(继承体系):子类 ==> 父类 ==> 爷

    注:

    1,如果想用这个体系的所有功能用最底层的类创建对象
    2,如果想看这个体系的共性功能,看最顶层的类

     1 class Demo2_Extends {
     2     public static void main(String[] args) {
     3         DemoC d = new DemoC();
     4         d.show();
     5     }
     6 }
     7 class DemoA {
     8     public void show() {
     9         System.out.println("DemoA");
    10     }
    11 }
    12 
    13 class DemoB extends DemoA {
    14     public void method() {
    15         System.out.println("DemoB");
    16     }
    17 }
    18 
    19 class DemoC extends DemoB {
    20     public void print() {
    21         System.out.println("DemoC");
    22     }
    23 }
    例子

    继承的注意事项

    1,子类只能继承父类所有非私有的成员(成员方法和成员变量)

    2,子类不能继承父类的构造方法,但是可以通过super关键字去访父类构造方法

    3,不要为了部分功能二区继承

    继承其实体现的是一中关系:“is a”

    假设:如果有两个类A,B。只有他们符合A是B的一种,或者B是A的一种,就可以考虑使用继承。

  • 相关阅读:
    codeforces 455B A Lot of Games(博弈,字典树)
    HDU 4825 Xor Sum(二进制的字典树,数组模拟)
    hdu 1800 Flying to the Mars(简单模拟,string,字符串)
    codeforces 425A Sereja and Swaps(模拟,vector,枚举区间)
    codeforces 425B Sereja and Table(状态压缩,也可以数组模拟)
    HDU 4148 Length of S(n)(字符串)
    codeforces 439D Devu and Partitioning of the Array(有深度的模拟)
    浅谈sass
    京东楼层案例思维逻辑分析
    浅谈localStorage和sessionStorage
  • 原文地址:https://www.cnblogs.com/Sherwin-liao/p/10919780.html
Copyright © 2011-2022 走看看