面向对象笔记

  1. 面向过程(POP)与面向对象(OOP)的区别:
    二者都是一种思想,面向对象是相对于面向过程而言的。面向过程,强调的是功能行为,以函数为最小单位,考虑怎么做。
    ①面向对象,将功能封装进对象,强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。
    ②面向对象更加强调运用人类在日常的思维逻辑中采用的思想方法与原则,如抽象、分类、继承、聚合、多态等。
  2. 面向对象的三大特征
    继承
    多态
    封装
  3. 经典案例—人把大象装进冰箱
    在这里插入图片描述
package day0311;

public class Elephant {
    String name;
    int age;
    double weight;
    public void eat(){
        System.out.println("我叫"+name+"我今年"+age+"岁,我正在吃饭!");
    }
}
package day0311;

public class ElephantDemo {
    public static void main(String[] args) {
        Elephant elephant = new Elephant();
        elephant.age = 10;
        elephant.name = "Tom";
        elephant.weight = 1000.5;
        elephant.eat();

        System.out.println("==============");
        Elephant elephant1 = new Elephant();
        elephant.age = 18;
        elephant.name = "Jack";
        elephant.weight = 47;
        elephant.eat();

        Fridge fridge = new Fridge();
        //让冰箱打开
        fridge.open();
        //让冰箱装大象
        fridge.input(elephant);
        //让冰箱关闭
        fridge.close();

    }
}
 
package day0311;

public class Fridge {
    String brand;
    public void open(){
        System.out.println("打开冰箱");
    }
    public void input(Elephant elephant){
        System.out.println(elephant.name+"被装进来了");
    }
    public void close(){
        System.out.println("冰箱关闭");
    }
}
  1. 练习-1
package day0315.day0316;

public class Cycle {
    private double r;
    /*
    1.声明时初始化
    2.final static 这个变量就变成了一个常量
     */
    private final static  double PI = 3.14;

    public Cycle() {
    }
    //this 当前对象的引用
    public Cycle(double r) {
        this.r = r;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }
    //没有static 叫实例方法
    public double getArea(){
        return PI*r*r;
    }

    /*
    1.静态方法不能访问实例方法和实例变量
    2.实例方法能访问静态方法和静态变量
     */
    public static void main(String[] args) {
        //System.out.println(r);
        Cycle cycle = new Cycle(10);
        cycle.getArea();
        //运行结果0.0
        System.out.println(cycle.getArea());
    }
}
package day0315.day0316;

public class CycleDemo {
    public static void main(String[] args) {
        /*
        第一种方法set
        Cycle cycle = new Cycle();
        cycle.setR(1);
        */
        Cycle cycle = new Cycle(2);
        //运行结果12.56
        System.out.println(cycle.getArea());

    }
}

练习-2

package day0315.day0316;
/*
1.建类
2.写属性,方法
3.封装属性
4.getter setter
5.构造函数
 */
public class Student {
    private long no;
    private String name;
    private String school;
    private float totalScore;

    public Student() {
    }

    public Student(long no, String name, String school) {
        this.no = no;
        this.name = name;
        this.school = school;
    }

    public Student(long no, String name, String school, float totalScore) {
        /*
        1.下面两行代码不能交换顺序,逻辑不合理
        2.通过this,来访问当前的对象
        3.可以访问构造函数 this();
        4.访问构造函数必须写在第一行
         */
        this(no,name,school);
        this.totalScore = totalScore;
    }

    public long getNo() {
        return no;
    }

    public void setNo(long no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public float getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(float totalScore) {
        this.totalScore = totalScore;
    }

    void display(){
        System.out.println(getName()+getTotalScore());
    }
}
package day0315.day0316;

public class StudentDemo {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("千玺");
        student.setNo(127);
        student.setSchool("重庆");
        student.setTotalScore(700);

        Student student1 = new Student(126,"王源","重庆",700);

        /*
        运行结果:
        千玺 100.0
        王源100.0
        */
        student.display();
        student1.display();

    }
}

练习-3

package day0315.day0316;
/*
初始化的顺序:
1.声明时初始化
2.静态代码块
3.实例代码块
4.构造函数
5.setter
 */
public class StaticDemo {
    static String url = "localhost";
    static String pwd = "123456";
    /*静态代码块 */
    static{
        System.out.println(url);
        //读一个配置文件
        url ="localhost1";
        System.out.println("静态代码块"+url);
    }
    /*实例代码块*/
    {
        System.out.println("实例代码块");
    }
    public StaticDemo(){
        System.out.println("构造函数");
    }
}
package day0315.day0316;

public class StaticTest {
public static void main(String[] args) throws Exception {
    //StaticDemo staticDemo = new StaticDemo();
    //加载类
    /*
    运行结果: localhost
              静态代码块localhost1
*/
    Class.forName("day0315.day0316.StaticDemo");

}
        }

练习-4

package day0315.day0316.PigDemo;

public class Pig {
    private double weight;
    private String color;
    private String type;
    //构造无参
    public Pig() {
    }

    //构造全参
    public Pig(double weight, String color, String type) {
        this.weight = weight;
        this.color = color;
        this.type = type;
    }

    //get set
    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void show(){

    }
}
package day0315.day0316.PigDemo;

public class PigFarm {
    private Pig[] pigs;
   public void showPig(){
      for(int i =0;i<pigs.length;i++){
          System.out.println(pigs[i].getColor()+pigs[i].getWeight());
      }

   }
    //get set
    public Pig[] getPigs() {
        return pigs;
    }

    public void setPigs(Pig[] pigs) {
        this.pigs = pigs;
    }
    //有参
    public PigFarm(Pig[] pigs) {
        this.pigs = pigs;
    }
    //无参
    public PigFarm() {
    }
}
package day0315.day0316.PigDemo;

public class PigDemo {
    public static void main(String[] args) {
        PigFarm pigFarm = new PigFarm();
        Pig pig = new Pig(20,"黑色","小香猪");
        Pig pig1 = new Pig(18,"黑白色","小猪");
        Pig pig2 = new Pig(21,"白色","佩奇");

        int[]ay = new int[10];
       // Pig[] pg = new Pig[10];
        Pig[] pg ={pig,pig1,pig2};

        pigFarm.setPigs(pg);
        pigFarm.showPig();
        /*
        运行结果:
        黑色20.0
        黑白色18.0
        白色21.0
        */
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值