Java_面向对象01_初始面向对象_20210902

本文介绍了Java中的类和对象的概念,强调对象是类的实例化表现,类是对象的类型。讨论了属性和方法,其中属性代表对象的特征,方法表示对象的行为。接着,讲解了如何创建类和实例化对象,包括类的命名规范和实例化过程。文章还探讨了单一职责原则,强调一个类应只有一个引起功能变化的原因。最后,提到了new关键字在对象实例化中的作用以及构造器的使用,包括构造器的定义和调用规则。
摘要由CSDN通过智能技术生成

类和对象

对象:万物皆对象,现实当中存在的东西皆是对象。真正看的到摸得着的具体实体,真正干活的也是对象。

类:模型,确定对象将会拥有的特征(属性)和行为(方法),一种概念,一种模型。

--对象是类的实例化表现

--类是对象的类型

--对象是特定类型的数据。

先定义类---->通过类,实例化对象

属性和方法

属性:对象拥有的各种静态特征。

方法:对象具有的各种动态行为

 创建类和实例化对象

创建类

众所周知java面向对象编程的大概思路是:根据需求-->创建类--->根据类实例化对象,实现需求

创建包

包的命名规范倒着写的域名

在包里面创建类 

 类的命名规则如CatTest

以下是创建类的例子:

package com.company;
/*
*宠物猫类
* @auther dongdong
* */
public class Cat {
    //成员属性:昵称、年龄、体重、品种
    String name;//名字
    int month;//年龄
    double weight;
    String species;
    //方法:跑、吃东西
    //跑动的方法
    public void run(){
        System.out.println("小猫快跑");
    }
    public void run(String name){
        System.out.println(name+"快跑");
    }
    //吃东西的方法
    public void eat(){
        System.out.println("小猫吃鱼");
    }
}

实例化对象 

 根据创建好的类,创建CatTest测试我们写好的Cat类

package com.company;

public class CatTest {
    public static void main(String[] args){
        //对象实例化
        Cat one = new Cat();
        //测试方法
        one.eat();
        one.run();
        //测试属性
        System.out.println(one.name);
        System.out.println(one.month);
        System.out.println(one.species);
        System.out.println(one.weight);
        System.out.println("对象初始化的时候会给属性加载默认值");
        //给对象的属性赋值
        one.name="花花";
        one.month=2;
        one.species="英国短毛";
        one.weight=1000;
        System.out.println(one.name);
        System.out.println(one.month);
        System.out.println(one.species);
        System.out.println(one.weight);
        one.run(one.name);
    }
}

单一职责原则

面向对象的重要原则:一个类呢有且只有一个引起功能变化的原因。简单来说一个类只让它干一件事。

如果一个类承担的功能越多,交融、耦合性越高

被复用的可能性越低。

且当中一个职责发生变化,可能影响其他职责的变化。

new关键字的理解

实例化对象的过程可以分为两块儿

声明对象 Cat one,声明对象是在栈当中创建,

实例化对象 new Cat(),在堆当中创建,

通过=将堆的内存地址存到栈里面建立关系。

通过这种方法实例化出来的对象有以下特征:
//two 和 one 栈中是同一块儿空间,只有堆中是不同地址
//所以修改two的属性one的属性也跟着变了

package com.company;

public class CatTest {
    public static void main(String[] args){
        //对象实例化
        Cat one = new Cat();
        Cat two = one;
        //测试方法
        one.eat();
        one.run();
        //测试属性
        System.out.println(one.name);
        System.out.println(one.month);
        System.out.println(one.species);
        System.out.println(one.weight);
        System.out.println("对象初始化的时候会给属性加载默认值");
        //给对象的属性赋值
        one.name="花花";
        one.month=2;
        one.species="英国短毛";
        one.weight=1000;
        System.out.println(one.name);
        System.out.println(one.month);
        System.out.println(one.species);
        System.out.println(one.weight);
        one.run(one.name);
        //two 和 one 栈中是同一块儿空间,只有堆中是不同地址
        //所以修改two的属性one的属性也跟着变了
        two.name="毛毛";
        two.month=4;
        two.species="英国短毛";
        one.weight=1000;
        System.out.println(one.name);
        System.out.println(one.month);
        System.out.println(one.species);
        System.out.println(one.weight);
        one.run(one.name);
    }
}

构造器 

new关键字的好搭档,不能被对象单独调用。只能在对象实例化的时候调用

构造方法与类名相同且必须相同,没有返回值

没有指定构造方法时,系统会自动添加无参构造方法

有指定构造方法,无论是有参、无参构造方法都不会自动添加无参构造方法

一个类中可以有多个构造方法

调用构造器的代码实例:

package com.company;

public class CatTest {
    public static void main(String[] args){
        //对象实例化
       Cat one = new Cat();
       Cat two = new Cat("huahua");
       two.run();
    }
}
package com.company;
/*
*宠物猫类
* @auther dongdong
* */
public class Cat {
    //成员属性:昵称、年龄、体重、品种
    String name;//名字
    int month;//年龄
    double weight;
    String species;

    public Cat(){
        System.out.println("我是无参构造器");
    }
    public Cat(String name){
        System.out.println("我是带参构造器");
    }
    //方法:跑、吃东西
    //跑动的方法
    public void run(){
        System.out.println("小猫快跑");
    }
    public void run(String name){
        System.out.println(name+"快跑");
    }
    //吃东西的方法
    public void eat(){
        System.out.println("小猫吃鱼");
    }
}

 构建带参数构造器

变量作用域 和 变量的就近原则

this关键字 

可以看出来实例化的时候是有默认赋值的。

this关键字还可以这样用,调用对象拥有的方法 

  还有几个需要注意的地方

1.如果刚好有函数和类名相同,这个函数也会被执行,不会执行构造器方法,因为构造器方法只会在实例化配合new字段的时候被调用。

不会真的有正常人这么写把!

 构造方法的调用在,只能在同一个类的构造器里调用,如下:

且要写在第一行。

表示调用了无参构造器,可以断点查看一下。 

package com.company;

public class CatTest {
    public static void main(String[] args){
        //对象实例化
       Cat one = new Cat();
       Cat two = new Cat("huahua");
       Cat three = new Cat("xiaohuang",10,2000,"keji");
//       three.run(three.name);
//       two.run();
//       one.run();
       one.Cat();
    }
}
package com.company;
/*
*宠物猫类
* @auther dongdong
* */
public class Cat {
    //成员属性:昵称、年龄、体重、品种
    String name;//名字
    int month;//年龄
    double weight;
    String species;

    public Cat(){
        System.out.println("我是无参构造器");
    }
    public Cat(String name){
        this();
        System.out.println("我是带参构造器");
    }
//    public void Cat(){
//        System.out.println("我只是哥普通方法,恰好叫Cat");
//    }
    public Cat(String name,int month,double weight,String species){
        this("miao");
//        name = name;
        this.name = name;
        this.month = month;
        this.weight = weight;
        this.species = species;
    }
    //方法:跑、吃东西
    //跑动的方法
    public void run(){
        this.eat();
        System.out.println("小猫快跑");
    }
    public void run(String name){
        System.out.println(name+"快跑");
    }
    //吃东西的方法
    public void eat(){
        System.out.println("小猫吃鱼");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值