Java面向对象设计

面向对象编程(oop)

定义

以类的方式组织代码,以对象的组织封装数据

  • 抽象
  • 三大特性
  1. 封装
  2. 继承
  3. 多态
  • 先有对象,后有类

回顾方法

package com.oop.Demo01;

import java.io.IOException;

// Demo01 类
public class Demo01 {
    //main 方法
    public static void main(String[] args) {

    }
    // return 返回一个值,结束类
    public String sayHello(){
        return "hello world";
    }
    public int Max(int a,int b){
        return a>b?a:b;//三元运算符
    }
    public void readFile(String file) throws IOException{
        
    }
}

package com.oop.Demo01;

public class Demo02 {
    public static void main(String[] args) {
        //实例化这个类
        //对象类型 对象名= 对象值
        student student=new student();

        student.say();
    }
//和类一起加载的
    public static void a(){
       // b();
}
// 类实例化之后才存在
public void b(){

}
}
package com.oop.Demo01;

public class student {

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

package com.oop.Demo01;

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;
    }
}

package com.oop.Demo01;
// 值传递
public class Demo04 {
    public static void main(String[] args) {
        int a=1;
        System.out.println(a);
        Demo04.changs(a);
        System.out.println(a);
    }
    public static void changs(int a){
        a=10;
    }
}

package com.oop.Demo01;
//引用传递:对象,本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        person person = new person();
        System.out.println(person.name);
        Demo05.change(person);
        System.out.println(person.name);
    }
    public static void change(person person){
       //person 是一个对象:指向  person person = new person(); 这是一个具体的人,可以改变属性
        person.name="split";
    }
}
// 定义一个person类,有一个属性:name
class person{
    String name;  //null
}

类和对象的创建

  • 类是一种抽象的数据类型,他是对某一类事物整体描述/定义,但是并不是代表某一个具体的事物

  • 对象是抽象概念的具体实例

  • 使用new关键词创建对象

package com.oop.Demo02;
// 一个项目应该只有一个main方法
public class Application {
    public static void main(String[] args) {
        //类,抽象的,实例化
        //类实例化后悔返回一个自己的对象
        //student对象就是一个student类的具体实例
       student xiaohong= new student();
        student xiaoming =new student();
        xiaoming.name= "小明";
        xiaoming.age=2;
        xiaohong.name= "小红";
        xiaohong.age=2;
        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);
    }
}

package com.oop.Demo02;
//学生类
public class student {
    //属性
    String name;
    int age;
    //方法
    public void study(){
        System.out.println(this.name+"在学习");
    }
}

构造器

  • 必须和类的名字相同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值