面向对象

  • 方法的调用
  • 本质是值传递
  • public class Demo1{ public static void main(String[] args) { int a = 1; Demo1.change(a); System.out.println(a); } public static void change(int a){ a=10; } }
  • 引用传递:对象传递,本质还是值传递

一个类中可以有很多class,只能有一个public class

public class Demo1 {
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println(student.name);//null
        Demo1.change(student);
        System.out.println(student.name);//tom
    }
    public static void change(Student student){
        student.name="tom";
    }
}
class Student {
    String name ;
}

类和对象:类是抽象化得对象,对象是实例化的类

/**
 * 类:抽象的
 * 实例化会返回一个自己的对象
 * student对象就是Student类的具体实例
 */
public class Student {
    String name;
    int age;
  public void say(){
   System.out.println("我叫:"+this.name);
  }
@Override 
public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}';
}
public class Demo1 {
    public static void main(String[] args) {
        Student student1 = new Student();//具体的对象
        student1.name="小红";
        student1.age=3;
        System.out.println(student1.toString());
        Student student2 = new Student();
        student1.name="小丽";
        student1.age=4;
        System.out.println(student2.toString());
    }
}

 

posted @ 2022-03-25 20:07  格兰芬多剁剁剁  阅读(31)  评论(0)    收藏  举报