JAVASE基础模块七(面向对象)

JAVASE基础模块七

面向对象

  • 面向过程是面向具体的步骤与过程 把每一个过程与步骤完成 然后由这些功能方法相互调用 完成需求

  • 面向对象 将步骤与功能进行封装 根据不同的功能 进行不同的封装 功能类似的封装到一起

  • 面向对象思想特点

    1. 复杂事情简单化
    2. 是一种更符合我们思想习惯的思想 懒人思想
    3. 从执行者变成了指挥者 角色发生了转换
  • 面向对象特征

    1. 封装 encapsulation
    2. 继承 inheritance
    3. 多态 polymorphism
  • 面向对象的开发

    1. 不断的创建对象 使用对象 指挥对象做事情
  • 描述现实事物

    1. 事物 属性+功能
    2. 类 成员变量+成员方法
  • 面向对象的描述

    1. class 定义一个类 类体现的就是封装的思想 封装成员属性与成员功能 是JAVA语言中的最小单位

    2. 成员变量:定义在类中方法外的变量

    3. 成员方法:

    4. 定义好的类 创建一个测试类 提供一个主方法 作为入口 在主方法中使用这个类

    5. 类是一个抽象的概念 不能直接使用 首先进行实例化 实例化完毕之后 才能使用

    6. 实例化 就是创建该类的对象 然后使用该类的对象 去调用类中的属性和方法

    7. 创建对象 借助关键字new来进行类的实例化

    8. 创建该类的对象的语法

       public class XianShi {
       XianShi x=new XianShi();}
      
    9. 可以通过对象名 来调用类中的属性与方法

      public class XianShi {
          String pinpai;
          int price;
          char color;
          double size;
          public void eat() {
              System.out.println("测试");}
          public void sleep() {
              System.out.println("测试");}
          public void fallInLove() {
              System.out.println("测试");}
          public static void main(String[] args) {
              XianShi x = new XianShi();
              x.pinpai = "apple";
              x.price = 4999;
              x.color = '红';
              System.out.println(x.pinpai);
              System.out.println(x.price);
              System.out.println(x.color);
              x.eat();
              x.fallInLove();
              x.sleep();}}
      
      运行结果:
      apple
      4999
      红
      测试
      测试
      测试
      Process finished with exit code 0
      
    10. 对象的覆盖

      public class DuiXiang {
          String name = "配狗";
          int age;
          public void eat() {
              System.out.println("吃饭");}
          public void sleep() {
              System.out.println("睡觉"); }}
      class xxx{
          public static void main(String[] args) {
              DuiXiang z=new DuiXiang();
              z.name="pg";
              z.age=22;
              z.eat();;
              z.sleep();
              DuiXiang zx=new DuiXiang();
              zx.name="bz";
              zx.age=22;
              DuiXiang zxz=zx;
              zxz.name="cd";
              zxz.age=88;
              System.out.println(zxz.name);
              System.out.println(zxz.age);
              System.out.println(z.name);
              System.out.println(z.age);
              System.out.println(zx.name);
              System.out.println(zx.age);}}
      
      运行结果:
      吃饭
      睡觉
      cd
      88
      pg
      22
      cd
      88
      Process finished with exit code 0
      
  • 局部变量与全局变量的区别

    1. 局部变量:定义在方法中的变量或方法声明声明上的变量 形参
    2. 成员变量:定义在类中方法外的变量
    3. 局部变量属于方法 岁方法的调用而产生 随着方法的调用完毕而消失
    4. 成员变量属于对象 实例变量 随着对象的创建而产生 随着对象的回收而消失
    5. 局部变量必须赋值才能使用
    6. 成员变量有默认值
    7. 局部变量存在栈内存 成员变量存在堆内存
  • 同名问题

    1. 当局部变量与全局变量同名时 变量的访问原则遵循就近原则

    2. 在方法内部这个局部范围内照这个变量 找到就使用 找不到就去成员范围内找 找到就使用 找不到就报错

      public class BianLiang {
          String name="bz";
          int age;
          public static void show() {
              String name="pg";
              System.out.println(name);}}
      class Test {
          public static void main(String[] args) {
      BianLiang s=new BianLiang();
      s.name="dd";
      s.show();}}
      
      运行结果:
      pg
      Process finished with exit code 0
      
      可以将多个类并列到一个JAVA文件中 但是 全局修饰符 public只能有一个 最好一个类一个.Java文件
  • 参数传递问题

    1. 基本类型作为参数传递 属于值传递 形参的改变不影响实参

    2. 引用类型作为参数传递 属于引用传递 传递的是地址值 形参会影响实参

      public class CanShu {
          public static void main(String[] args) {
           int num=10;
           Student student=new Student();
           test(student,num);
              System.out.println(num);
              System.out.println(student.num); }
          public static void test(Student student,int num) {
               num+=20;
               student.num=num; }}
      
      运行结果:
      10
      30
      Process finished with exit code 0
      
      
  • 匿名对象

    public class NiMingDuiXiang {
        public static void main(String[] args) {
            Dog s=new Dog();
            s.eat();
            s.eat();
          // 同一个对象 调用了eat()方法两次
            System.out.println(new Dog().num);
            //两个匿名函数 各调用了一次
            new Dog().eat();
            new Dog().eat();
            //当你只想调用一次的时候 可以使用匿名函数
            //匿名对象可以作为参数传递
            int num=30;
            Dog sx=new Dog();
            test(sx,num);
            test(new Dog(),num);
            System.out.println(sx.num);
            System.out.println(new Dog().num);}
        public  static void test(Dog s,int num){
            num+=20;
            s.num=num;}}
    class  Dog{
        int num=11;
        public  void eat(){
            System.out.println("pg tcdj");}}
    
    
    运行结果:
    pg tcdj
    pg tcdj
    11
    pg tcdj
    pg tcdj
    50
    11
    进程已结束,退出代码0
    
    
    1. 当你只想调用一次的时候 可以使用匿名函数
    2. 匿名对象可以作为参数传递
  • d

    1. 我们采用 对象名.成员变量=值 这种方式不能排除一些不合理的数据

    2. 用private 可以进行屏蔽

    3. private 私有的 权限修饰符 可以修饰成员变量 也可以修饰成员方法 被修饰的成员 只能在本类中访问

      public class Phone {
          String pingPai;
          int price;
          char color;
      
          public void setPingPai(String x) {
              pingPai = x;
      
          }
      
          public void setPrice(int x) {
              price = x;
      
          }
      
          public void setColor(char x) {
              color = x;
      
          }
      
          public String getPingPai() {
              return pingPai;
      
          }
      
          public int getPrice() {
              return price;
      
          }
      
          public char getColor() {
              return color;
      
          }
      
          public void show() {
              System.out.println(pingPai);
              System.out.println(color);
              System.out.println(price);
          }
      }
      class TestPhone{
          public static void main(String[] args) {
              Phone z=new Phone();
              z.color='粉';
              z.price=6655;
              z.pingPai="Apple";
              char c=z.getColor();
              int v=z.getPrice();
              String n=z.getPingPai();
              z.show();
          }
      }
      
      
      运行结果:
      Apple
      粉
      6655
      
      进程已结束,退出代码0
      
      
    4. 方法 权限修饰符 状态修饰符 返回值类型 方法名(形参列表){方法体}

    5. public 公有的 权限修饰符 可以修饰成员变量 也可以修饰成员方法 范围最大 任何地方都能调用

  • 权限修饰符 所代表的范围由大到小排列

    1. public

    2. protected

    3. 缺省的

    4. private

          public String a;
          protected int age;
          String c;
          private int pp;
      
      
  • this关键词

    1. 代表该类的一个引用 理解为该类的对象 哪个对象调用这个方法 方法中的this就代表那个对象

      public class Cat {
          private String name;
          private char sex;
          private int age;
      
          public void setName(String name) {
              this.name = name;
          }
      
          public void setSex(char sex) {
              this.sex = sex;
          }
      
          public void setAge(int age) {
              this.age = age;
          }
      
          public void show() {
              System.out.println("姓名:" + name);
              System.out.println("性别:" + sex);
              System.out.println("年龄:" + age);
          }
      }
      
      class TestCat {
          public static void main(String[] args) {
              Cat z = new Cat();
              z.setAge(3);
              z.setName("小公主");
              z.setSex('母');
              z.show();
          }
      
      
      
      运行结果:
      姓名:小公主
      性别:母
      年龄:3
      
      进程已结束,退出代码0
      
      
  • 快速生成 set与get函数 Alt+Insert

    待续…

猜你喜欢

转载自blog.csdn.net/cx9977/article/details/107443528