JAVA基础学习20171214-面向对象

1.java面向对象的三大特征:封装,继承,多态

2.类和对象
类是模子,确定对象将拥有的特征(属性)和行为(方法)
对象是类的实例表现
类是对象的类型
对象是特定类型的数据

3.属性和方法
属性:对象具有的各种静态特征,对象有什么
方法:对象具有的各种动态行为,对象能做什么

4.类和对象的关系

-抽象的概念
-模板
对象
-一个看的到、摸得着的具体实体

5.单一职责原则:一个类只干一件事
如果在一个类中承载的功能越多,交融、耦合性就越高,被复用的可能性就越低

代码:

package com.imooc.animal;
/**
 * 宠物猫
 * @author dingguanyi
 *
 */
public class Cat {
    //成员属性:昵称、年龄、体重、品种
    String name;//昵称 String类型默认值null
    int month;//年龄 int类型默认值0
    double weight;//体重 double类型默认值0.0
    String species;//品种

    //成员方法:跑动、吃东西
    //跑动方法
    public void run(){
        System.out.println("小猫快跑");
    }
    //快跑方法重载
    public void run(String name){
        System.out.println(name+"快跑");
    }

    //吃东西的方法
    public void eat(){
        System.out.println("小猫吃鱼");
    }
}

package com.imooc.animal;

public class CatTest {
    public static void main(String[] args){
        //对象实例化
        Cat one=new Cat();
        //测试
        one.eat();
        one.run();
        one.name="花花";
        one.month=2;
        one.weight=1000;
        one.species="英国短毛猫";
        System.out.println("昵称:"+one.name);
        System.out.println("年龄:"+one.month);
        System.out.println("体重:"+one.weight);
        System.out.println("品种:"+one.species);
        one.run(one.name);
    }
}

6.对象实例化
实例化对象的过程可以分为两部分:
-声明对象 Cat one 在栈空间存储实例化的内存地址
-实例化对象 new Cat(); 在堆空间存储实例化的数据

代码:

package com.imooc.animal;

public class CatTest1 {
    public static void main(String[] args){
        //对象实例化
        Cat one=new Cat();
        Cat two=new Cat();//在堆中开辟空间存储新对象
        Cat three=one;//在栈中开辟空间存储原对象的地址
//      Cat one;//声明对象
        //测试
        one.name="花花";
        one.month=2;
        one.weight=1000;
        one.species="英国短毛猫";
        two.name="凡凡";
        two.month=1;
        two.weight=800;
        two.species="中华田园猫";
        three.name="99";
        three.month=99;
        three.weight=99;
        three.species="99";
        System.out.println("昵称:"+one.name);
        System.out.println("年龄:"+one.month);
        System.out.println("体重:"+one.weight);
        System.out.println("品种:"+one.species);
        System.out.println("--------------------------");
        System.out.println("昵称:"+two.name);
        System.out.println("年龄:"+two.month);
        System.out.println("体重:"+two.weight);
        System.out.println("品种:"+two.species);
        System.out.println("--------------------------");
        System.out.println("昵称:"+three.name);
        System.out.println("年龄:"+three.month);
        System.out.println("体重:"+three.weight);
        System.out.println("品种:"+three.species);
    }
}

7.构造方法(又叫构造函数、构造器)
1)构造方法与类同名且没有返回值
2)构造方法的语法格式
public 构造方法名(){
}
3)只能在对象实例化的时候调用
4}当没有指定构造方法时,系统会自动添加无参的构造方法
5)当有指定构造方法,无论是有参、无参的构造方法,都不会自动添加无参的构造方法
6)一个类中可以有多个构造方法

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值