面向对象编程(OOP)
面向对象编程的本质就是:以类的方式组织代码, 以对象的组织(封装)数据
特征:
封装
继承
多态
准备:方法
◆方法的定义
◆修饰符
◆返回类型
◆break 和return的区别
◆方法名
◆参数列表
◆异常抛出
◆方法的调用
◆静态方法
◆非静态方法
◆形参和实参
◆值传递和引用传递
◆this关键字
简单 sayHello
函数
/**
* 修饰符 返回值类型 方法名(.参数.){
* //方法体
* return 返回值;
* }
*/
public String sayhello(){
return "hello world!";
}
void
类型 不需要写返回值 或者是空
public void hello(){
return; //返回是空值
}
简单比大小方法
public static int max(int a, int b){
return a>b ? a : b; //三元运算符!!!!
}
break
和 return
的区别
break:
跳出 switch,结束循环
return:
结束方法,返回一个结果
方法名 : 驼峰命名法 见名知意
方法的调用
静态方法 //static
非静态方法 //无 static
非静态方法
//非静态方法
//Student.java
//学生类
public class Student {
//非静态方法
public void hello(){
System.out.println("我是学生");
}
}
//Demo2.java
public class Demo2 {
public static void main(String[] args) {
//实例化学生类 new
//对象类型 对象名 = 对象值
Student student = new Student();
student.hello();
}
}
形参和实参
public class Demo3 {
public static void main(String[] args) {
//实际参数和形式参数的类型要对应
int add = new Demo3().add(2, 3);
System.out.println(add);
}
public int add(int a, int b){
return a + b;
}
}
添加 static
修饰符
public class Demo3 {
public static void main(String[] args) {
//实际参数和形式参数的类型要对应
int add = Demo3.add(3, 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); //调用 change 方法
System.out.println(a); //1
}
//返回值为空
public static void change(int a){
a = 10;
}
}
传入输出的值也是 1
引用传递
// 引用传递:传递对象,本质还是值传
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); //Muz1
}
public static void change(Person person){
//person 是一个对象, 指向的是 -----> Person person = new Person();这是一个具体的人,可以改变属性
person.name = "Muz1";
}
}
//定义了一个 Person 类,有一个 name 属性
class Person{
String name;
}
创建对象
学生类
//学生类
public class Student {
//属性
String name;//null
int age;//0
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
程序类
package com.OOP.Demo2;
//一个项目只有一个 main 方法
public class Application {
public static void main(String[] args) {
//类: 抽象的,需要实例化
//类实例化之后会返回一个自己的对象
//student 对象就是 Student 类的具体实例
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name = "小明";
xiaoming.age = 3;
xiaoming.study();//小明在学习
System.out.println(xiaoming.name);//小明
System.out.println(xiaoming.age);//3
xiaohong.name = "小红";
xiaohong.age = 3;
xiaohong.study();//小红在学习
System.out.println(xiaohong.name);//小红
System.out.println(xiaohong.age);//3
}
}
构造器
构造器
也称构造方法
//特点
1.必须和类的名字相同
2.必须没有返回类型,也不能写void
Demo
//Person.java
//java ---> class
public class Persron {
}
//Application.java
public class Application {
public static void main(String[] args) {
//实例化一个Persion对象,然后编译
Persron persron = new Persron();
}
}
实例化一个空类,然后编译输出
发现可以执行,但是Person类
里面没有东西,然后看一下编译后生成的Person.class
文件
//Person.class
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.OOP.Demo2;
public class Persron {
public Persron() { //自己生成Person方法
}
}
在Person.class
文件中自己生成了一个Person
方法
一个类即使什么也不写,也会存在一个方法
这就是一个简单的构造器
自己定义一个构造器
public class Persron {
//一个类即使什么也不写,也会存在一个方法
//显示的定义构造器
public Persron(){
}
}
构造器分为无参构造器和有参构造器
Demo
//Application.java
public class Application {
public static void main(String[] args) {
Persron persron = new Persron();
System.out.println(persron.name);
}
}
//无参构造器
//Person.java
public class Persron {
String name; //定义一个name;
//实例化初始值
//1.使用new关键字,本质是在调用构造器
//无参数构造
public Persron(){
this.name = "Mr.木Mu";
}
}
输出
有参构造器
//Person.java
public class Persron {
String name;
//有参构造:一旦定义了有参构造,无参就必须显式定义
public Persron(String name){
this.name = name;
}
}
这里的this.name指向就是 自己定义的 String name;
后面的name是传入的name参数
这时候的Application.java
就会有一个错误
提示我们缺少参数
为了不报错且可行,就把无参构造显式定义
//Person.java
public class Persron {
//显示的定义构造器
String name;
//实例化初始值
//无参数构造 显式定义
public Persron(){
}
//有参构造:一旦定义了有参构造,无参就必须显式定义
public Persron(String name){
this.name = name;
}
}
直接编译
//Application.java
public class Application {
public static void main(String[] args) {
//这里没有参数
Persron persron = new Persron();
System.out.println(persron.name);
}
}
传递参数编译
public class Application {
public static void main(String[] args) {
//传递参数
Persron persron = new Persron("Mr.木Mu");
System.out.println(persron.name);
}
}
简单过程:
最后输出结果
构造器的作用
1.使用new关键字,本质是在调用构造器
2.用来初始化值:(null,0)
//在IDEA里面的快捷键: ALT + Inster
完整Demo
//Application.java
//一个项目只有一个main方法
public class Application {
public static void main(String[] args) {
Persron persron = new Persron("Mr.木Mu");
System.out.println(persron.name);
}
}
//Person.java
import com.sun.jdi.event.StepEvent;
import javax.print.DocFlavor;
//java ---> class
public class Persron {
//一个类即使什么也不写,也会存在一个方法
//显示的定义构造器
String name;
//实例化初始值
//1.使用new关键字,本质是在调用构造器
//无参数构造
public Persron(){
// this.name = "Mr.木Mu";
}
//有参构造:一旦定义了有参构造,无参就必须显式定义
public Persron(String name){
this.name = name;
}
}
/**
* public static void main(String[] args) {
* Persron persron = new Persron("Mr.木Mu");
* System.out.println(persron.name);
* }
*
* @构造器:
* 1.和类名相同
* 2.没有返回值
* @作用:
* 1.new本质 实在调用构造器
* 2.初始化对象的值
* @注意点
* 1.定义有参之后,想使用无参,显示一个定义的无参
*/
创建对象与内存分析
//Application.java
package com.OOP.Demo3;
import com.OOP.Demo2.Pet;
public class Appliacation {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name = "旺财";
dog.age = 3;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
}
}
//Pet
package com.OOP.Demo3;
public class Pet {
String name;
int age;
//无参构造
public void shout(){
System.out.println("叫了一声");
}
}
封装
//Application.java
package com.OOP.Demo4;
/**
* 1.提高程序安全性
* 2.隐藏代码的实现细节
* 3.统一接口
* 4.增加系统的可维护性
*/
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.getName();
String name = s1.getName();
s1.setName("Muz1");
System.out.println(s1.getName());
s1.setAge(999);
System.out.println(s1.getAge());
}
}
//Student.java
package com.OOP.Demo4;
//类 private
public class Student {
//属性私有
private String name; //名字
private int id; //学号
private char sex; //性别
private int age; //年龄
//提供一些可以操作这个属性的方法
//提供一些public 的 get /set 方法
//get 获得这个数据
public String getName(){
return this.name;
}
//set 给这几个数据设置值
public void setName(String name){
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>120 || age<0){
this.age = 3;
}else {
this.age = age;
}
}
}
继承
//Application
package com.OOP.Demo5;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.say();
System.out.println(s1.money);
}
}
//Person
package com.OOP.Demo5;
//人 : 父类
public class Person {
//public
public int money = 1000000;
public void say(){
System.out.println("啊吧啊吧");
}
}
Student类
里面没有内容
//Student
package com.OOP.Demo5;
//Student is 人 : 子类
public class Student extends Person{
}
把 pubilc
换成 private
package com.OOP.Demo5;
//人 : 父类
public class Person {
//private
private int money = 1000000;
public void say(){
System.out.println("啊吧啊吧");
}
}
报错显示
添加 getter/setter
//Person
package com.OOP.Demo5;
//人 : 父类
public class Person {
//public
//prvate
//default
//protected
private int money = 1000000;
public void say(){
System.out.println("啊吧啊吧");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
ctrl+H
把 Person
类 清空
package com.OOP.Demo5;
//人 : 父类
public class Person {
}
可以看到有好多的方法
实际上都是Object类
中的方法
每个类都有Object类
Java只有单继承
super
super
用于调用父类变量
//Application
package com.OOP.Demo5;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("Application");
}
}
//Person
package com.OOP.Demo5;
//人 : 父类
public class Person {
protected String name = "Person";
}
//Student
package com.OOP.Demo5;
//Student is 人 : 子类
public class Student extends Person{
private String name = "Student";
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
}
执行Application.java
输出
//test()
public void test(String name){
System.out.println(name); //Application
System.out.println(this.name); //Student
System.out.println(super.name);//Person
}
修改函数
//Application
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test1();
}
}
//Person
package com.OOP.Demo5;
//人 : 父类
public class Person {
public void print(){
System.out.println("Person");
}
}
//Student
package com.OOP.Demo5;
//Student is 人 : 子类
public class Student extends Person{
public void print(){
System.out.println("Student");
}
public void test1(){
print();
this.print();
super.print();
}
}
输出结果
//test1()
public void test1(){
print(); //Student
this.print(); //Student
super.print();//Person
}
构造器
//Application
public class Application {
public static void main(String[] args) {
Student student = new Student();
}
}
//Person
public class Person {
public Person() {
System.out.println("Person无参构造执行了");
}
}
//Student
public class Student extends Person{
public Student() {
//隐藏代码,调用父类无参构造器
super();//必须放在子类的第一行
System.out.println("Student无参构造执行了");
}
}
super
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现于类的方法发或者构造法方法中
3.super和this不能同时调用构造方法
方法的重写
//Application
public class Application {
public static void main(String[] args) {
A a = new A();
a.test();
}
}
//A
public class A extends B{
public static void test(){
System.out.println("A=>test()");
}
}
//B
//重写都是方法的重写
public class B {
public static void test(){
System.out.println("B=>test()");
}
}
输出
在Application.java
中加入
public class Application {
public static void main(String[] args) {
A a = new A();
a.test(); //A
//父类的引用指向了子类
B b = new A();
b.test(); //B
}
}
运行
把 static
去掉
再次运行得到
即b是A new出来的对象,因此调用了A的方法
因为静态方法是类的方法,而非静态是对象的方法
有static时,b调用了B类的方法,因为b是用b类定义的
没有static时,b调用的是对象的方法,而b是用A类new的
/**
* 重写:需要有继承关系,子类重写父类的方法
* 1。方法名必须相同
* 2.参数列表必须胸痛
* 3.修饰符: 范围可以扩大 pulic > protected > default > private
* 4.抛出的异常 : 可以被缩小,但不能扩大: ClassNotFound ----> Exception
*/