属性+方法=类 物以类聚
面向对象编程本质:以类的方式组织代码,以对象的方式组织(封装)数据
面向对象三大特性:封装 继承 多态
package com.fj.oop.demo01;
//Demo01类
public class Demo01 {
//main方法
public static void main(String[] args) {
}
public String sayHello() {
return "hello,world";
}
public int max(int a, int b) {
return a > b ? a : b;//三元运算符
}
}
2. static 方法和普通方法调用区别
- student 类
package com.fj.oop.demo01;
//学生类
public class Student {
//方非静态法
public void say(){
System.out.println("学生说话了");
}
}
- 引用
package com.fj.oop.demo01;
public class Demo02 {
public static void main(String[] args) {
//实例化这个类 new
//对象类型 对象名 = 对象值;写完new Student(); 用alt + enter 快捷键可以直接出来前面的Student student =
Student student = new Student();
//和类一起加载,可以直接调用
a();
//new 实例化之后才存在,必须先new
Demo02 demo02 = new Demo02();
demo02.b();
}
//和类一起加载
public static void a(){
}
//new 实例化之后才存在
public void b(){
}
}
3. 形参和实参
package com.fj.oop.demo01;
public class Demo03 {
public static void main(String[] args) {
// 1,3 实参
int add = Demo03.add(1,3);
System.out.println(add);//4
}
//int a, int b 形参
public static int add(int a, int b){
return a + b;
}
}
4. 值传递
package com.fj.oop.demo01;
//值传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);//1
change(a);
System.out.println(a);//1
}
public static void change(int a) {
a = 10;
}
}
5. 引用传递:对象,本质还是值传递
package com.fj.oop.demo01;
//引用传递:对象,本质还是值传递
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);//null
change(person);
System.out.println(person.name);//ddd
}
public static void change(Person p) {
//Person 是一个对象,指向的是具体的人,可以改变属性
p.name = "ddd";
}
}
//定义一个person类,有一个属性:name
class Person {
String name;//null
}
3、 对象的创建和分析
1. 对象的创建
- Student类创建
package com.fj.oop.demo02;
//学生类
public class Student {
//属性:字段
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"学生在学习");
}
}
- Application 实例化类的创建
一个类,可以实例化无数个对象
package com.fj.oop.demo02;
//一个项目应该只存一个main方法
public class Application {
public static void main(String[] args) {
//类:抽象的,需要实例化
//类实例化后会返回一个自己的对象
//student,student1对象就是一个Student类的具体实例
Student student = new Student();
Student student1 = new Student();
student.name = "小米";
student.age = 12;
System.out.println(student.name);
System.out.println(student.age);
System.out.println("========================");
student1.name = "小李";
student1.age = 18;
System.out.println(student1.name);
System.out.println(student1.age);
}
}
2. 构造方法
构造方法特点:
-
必须和类的名字相同
-
必须没有返回值,也不能写void
-
Person 类说明
package com.fj.oop.demo02;
//一个类什么都不写,他也会存在一个方法(构造方法)
public class Person {
//显示的定义构造器
String name;
//实例化初始值
//1. 使用new关键字,本质是在调用构造器
//alt+insert 默认生成构造器
public Person() {
this.name = "sss";
}
//2. 有参构造:一旦定义了有参构造,无惨就必须显示定义
public Person(String name) {
this.name = name;
}
}
调用构造方法
package com.fj.oop.demo02;
//一个项目应该只存一个main方法
public class Application {
public static void main(String[] args) {
//类:抽象的,需要实例化
//new 实例化一个无参构对象
Person person = new Person();
System.out.println(person.name);//sss
//new 实例化有参构造
Person person1 = new Person("DDD");
System.out.println(person1.name);//DDD
}
}
四、 面向对象三大特性
五、 抽象类和接口
六、 内部类及OOP实战


