zoukankan      html  css  js  c++  java
  • java基础--第七天

    1Java中的参数传递问题。(理解内存图)

      基本类型:形式参数的改变对实际参数没有影响。

      引用类型:形式参数的改变直接影响实际参数。

    2:面向对象

      (1)面向对象:是基于面向过程的一种思想。

        面向过程:以函数为基础,关注实现过程。

        面向对象:以对象为基础,关注实现结果。

      (2)面向对象的思想特点:

        A:是一种更符合人们思考习惯的思想。

        B:把复杂的事情简单化了。

        C:把我们从执行者变成了指挥者。

        举例:

          买电脑。洗衣,做饭。旅游。把大象装进冰箱。[代码体现]

     1 /*
     2     将大象装进冰箱
     3 */
     4 class DaXiang {
     5     public  void in(){
     6         System.out.println("大象送进冰箱");
     7     }    
     8 }
     9 class BingXiang{
    10     public static void open(){
    11         System.out.println("打开冰箱");
    12     }
    13 
    14     public static void close(){
    15         System.out.println("关闭冰箱");
    16     }    
    17 }
    18 class TestDemo{
    19     public static void main(String[] args) {
    20 
    21         open();
    22         in();
    23         close();
    24 
    25         System.out.println("----------------");
    26         
    27         BingXiang bingXiang = new BingXiang();
    28         bingXiang.open();
    29         new DaXiang().in();//匿名调用
    30         bingXiang.close(); //BingXiang类不可用匿名类,效果可以出来,但是是两个类
    31     }
    32             //面向过程
    33         public static void open(){
    34             System.out.println("打开冰箱");
    35         }
    36 
    37         public static void close(){
    38             System.out.println("关闭冰箱");
    39         }
    40 
    41         public static void in(){
    42             System.out.println("大象送进冰箱");
    43         }
    44 }

      (3)事物是如何体现的呢?

        A:属性 有哪些特征

        B:行为 有哪些功能

      (4)类与对象的关系:

        把事物转换成类:

          A:成员变量

            定义在类中,方法外。

          B:成员方法

            和以前的区别是去掉static

        类:是相关的属性和行为的集合。是一个抽象的概念。

        对象:是某种事物的具体存在,具体的表现形式。

        举例:

          类:学生      对象:张三

      (5)案例:

        A:学生类

     1 /*
     2     测试类Student的使用,简单应用类中成员变量和成员函数
     3 */
     4 class Student
     5 {
     6     //姓名
     7     String name;
     8     //年龄
     9     int age;                        //年龄范围可以用更小,但是Java进行自我内存管理,
    10                                     //且short/byte类型数据会自动提升为int类型
    11     //性别
    12     char sex;
    13 
    14 
    15     //学习的方法
    16     public void study()
    17     {
    18         System.out.println("学生学习");
    19     }
    20     //吃饭的方法
    21     public void eat()
    22     {
    23         System.out.println("学生吃饭");
    24     }
    25         //睡觉的方法
    26     public void sleep()
    27     {
    28         System.out.println("学生睡觉");
    29     }
    30 }
    31 
    32 class StudentTest 
    33 {
    34     public static void main(String[] args) 
    35     {
    36         //创建对象
    37         Student s = new Student();
    38         //应用对象属性与方法
    39         System.out.println(s.name +"	"+s.age+"	"+s.sex);
    40         //更改对象属性
    41         s.name = "张三";
    42         s.age = 24;
    43         s.sex = '男';
    44         //应用对象属性与方法
    45         System.out.println(s.name +"	"+s.age+"	"+s.sex);
    46 
    47         s.study();
    48         s.eat();
    49         s.sleep();
    50     }
    51 }

        B:手机类

     1 /*
     2     需求:
     3         属性:品牌(brand)、价格(price)、颜色(color)
     4         
     5         行为:打电话(call)、发短信(sendMessage)、玩游戏(playGame)
     6 */
     7 class Phone
     8 {
     9     //品牌(brand)
    10     String brand;
    11     //价格(price)
    12     int price;//考虑当前钱不值钱的情形   ~_~
    13     //颜色(color)
    14     char color;
    15 
    16     //打电话(call)
    17     public void call()    //boolean  最好  call应该有对象
    18     {
    19         System.out.println("打电话");
    20     }
    21     //发短信(sendMessage)
    22     public boolean sendMessage(String s)
    23     {
    24         System.out.println("发出信息信息内容");
    25         if(s!=null)
    26         {
    27             System.out.println(s);
    28             return true;
    29         }
    30         else
    31             return false;
    32     }
    33     //玩游戏(playGame)
    34     public void playGame()
    35     {
    36         System.out.println("玩游戏");
    37     }
    38 }
    39 
    40 class PhoneDemo
    41 {
    42     public static void main(String[] args) 
    43     {
    44         //属性应用
    45         Phone p = new Phone();
    46         System.out.println(p.brand+"	"+p.price+"	"+p.color);
    47         p.brand = "华为";
    48         p.price = 1990;
    49         p.color = '白';
    50         System.out.println(p.brand+"	"+p.price+"	"+p.color);
    51 
    52         //方法应用
    53         p.call();
    54         boolean right = p.sendMessage("一定要成功!");
    55         if(right)
    56             System.out.println("发送信息成功!");
    57         else
    58             System.out.println("发送信息失败!");
    59         p.playGame();
    60     }
    61 }

        C:汽车类(内存图)

     1 /*
     2     需求:汽车:
     3         属性:品牌、价格、颜色
     4         行为:跑、停
     5 */
     6 class Car
     7 {
     8     //品牌
     9     String brand;
    10     //价格
    11     int price;
    12     //颜色
    13     String color;
    14 
    15     public void run()
    16     {
    17         System.out.println("汽车奔跑");
    18     }
    19 
    20     public void stop(int num)
    21     {
    22         if(num>120)
    23             System.out.println("汽车停下");
    24     }
    25 }
    26 
    27 class CarDemo
    28 {
    29     public static void main(String[] args) 
    30     {
    31         Car car = new Car();
    32         System.out.println(car.brand+"	"+car.price+"	"+car.color);
    33         car.brand = "奔驰";
    34         car.price = 1000000;
    35         car.color = "红色";
    36         System.out.println(car.brand+"	"+car.price+"	"+car.color);
    37 
    38         car.run();
    39         car.stop(140);
    40 
    41     }
    42 }

      (6)如何使用呢?

        A:创建对象

        格式:

          类名 对象名 = new 类名(); 

        B:使用成员变量和成员方法

          对象名.成员变量    对象名.成员方法

    3:匿名对象

      (1)就是没有名字的对象

      (2)应用场景

        A:对对象方法只调用一次;

        B:用对象作为实际参数

  • 相关阅读:
    大规模扩展casbin的一种方案
    casbin 性能优化的几点建议
    使用casbin进行应用访问控制
    使用xid 生成 唯一id
    jcsabin FilteredAdapter 逻辑处理问题
    jcasbin redis watcher 一些修改
    cube.js 0.27.31 一些新特性
    sdkman Stop! maven is not a valid candidate 类似问题解决
    lakefs 类似git 的文件对象存储
    readme.so docker 镜像
  • 原文地址:https://www.cnblogs.com/zhidianhcuan/p/4349061.html
Copyright © 2011-2022 走看看