day11面向对象编程

面向过程&面向对象

面向过程思想

1.步骤清晰简单,第一步做什么,第二步做什么。
2.面向过程适合处理一些较为简单的问题

面向对象思想

1.物以类聚,分类的思维模式,思考问题首先会解决问题需要那些分类,然后对这些分类进行单独思考,最后,才对某个分类下的细节进行面向过程的思索。
2.面向对象适合处理复杂的问题,适合处理需要多人协作的问题

面向对象编程(oop)

面向对象编程的本质就是:以类的方式组织代码,以对象的形式封装数据。
面向对象三大特性:封装、继承、多态。

方法如何去定义:
修饰符 返回值类型 方法名(参数类型  参数名){
  方法体
  return 返回值;
}
    public String sayhello(){
        return "hello world";//返回值必须与返回类型相同
    }
    public int max(int a,int b){
        return a>b?a:b;//是就a否就b
    }
    public void hello(){//如果是void的类型,那么返回值可以不写
        return;
    }
静态方法的调用
public class Demo02 {
    public static void main(String[] args) {
        Student.say();

    }
}


public class Student {
    //静态方法
    public static void say(){
        System.out.println("说话了");
    }
}

非静态方法调用
public class Demo02 {
    public static void main(String[] args) {
        //实例化这个类new
        //对象类型 对象名 = 对象值
        Student student = new Student();
        student.say();
    }
}


public class Student {
    //非静态方法
    public void say(){
        System.out.println("说话了");
    }
}

静态方法只能调用静态方法,非静态方法可以调用静态方法
public class Demo03 {
    public static void main(String[] args) {
        //实际参数与形式参数的类型要对应!
        int add = Demo03.add(1, 2);
        System.out.println(add);


    }
    public static int add(int a,int b){
        return a+b;
    }
}

//值传递
public class Demo4 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);//1
        Demo4.change(a);
        System.out.println(a);//1
    }
    //返回值为空
    public static void change(int a){
      a=10;
    }
}

//引用传递:对象,本质还是值传递。
public class Demo5 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);//null
        Demo5.change(person);
        System.out.println(person.name);//wjc


    }

    public static void change(Person person) {
	//person是一个对象:指向的---> Person person = new Person();这是一个具体的人,可以改变属性//
        person.name = "wjc";
    }
}
//定义一个类person类,有一个属性name。
class Person{
    String name;//null
}
posted @ 2022-03-07 23:52  行通方一  阅读(32)  评论(0)    收藏  举报