面对对象与回顾方法

面对对象

  • 面对对象编程(Obiect-Oriented Programming,OOP)
  • 面对对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据。
  • 抽象
  • 三大特性:
  1. 封装
  2. 继承
  3. 多态
  • 从认识论的角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象
  • 从代码运行角度考虑是先有类后有对象。类是对象的模版。

回顾方法及加深

  1. 方法的定义:
  • 修饰符
  • 返回值类型
  • break(跳出switch,结束循环)和return(方法结束)
  • 方法名(驼峰命名法,见名知意)
  • 参数列表(参数类型,参数名)…可变行参数
  • 异常抛出
public class Dem01 {
    public static void main(String[] args) {

    }
    /*
    修饰符  返回值类型 方法名(变量){
          //方法体
          return 返回值;
    }

     */
    //return 结束方法,返回一个结果!
    public String sayHello(){
        return "sayHello";
    }
    public void print(){
        return;
    }
    public int max(int a,int b ){
        //三元运算符,如果是a>b,返回 a ,否则返回 b
        return a>b ? a:b;
    }
    public void  readFile(String file) throws IOException{
    //读文件,抛出异常。    
    }
  1. 方法的调用:递归
  • 静态方法:有 static(和类一起加载的)
public class Dem02 {
    public static void main(String[] args) {
        Student.op();
    }
}
输出结果:
万叶:总会有地上的生灵,敢于直面雷霆的微光
public class Student {
    //静态方法调用
    public static void op(){
        System.out.println("万叶:总会有地上的生灵,敢于直面雷霆的微光");
    }
}
  • 非静态方法:无static(类实例化之后才存在)
public class Dem02 {
    public static void main(String[] args) {
       //实例化这个类new
        // 对象类型 对象名 = 对象值;
        Student student = new Student();
        student.op();
    }
}
输出结果:
万叶:总会有地上的生灵,敢于直面雷霆的微光
public class Student {
    //非静态方法调用
    public  void op(){
        System.out.println("万叶:总会有地上的生灵,敢于直面雷霆的微光");
    }
}
  • 形参和实参(形参和实参的数据类型要一致)
public class Dem03 {
    public static void main(String[] args) {
       //静态方法调用
        int add = Dem03.add(1,2);
        System.out.println(add);
    }
    public static int add(int a ,int b) {
        return a+b;
    }
}
输出结果:
3
public class Dem03 {
    public static void main(String[] args) {
       //非静态方法调用
       Dem03 dem03 = new Dem03();
       int add = dem03.add(1,2);
        System.out.println(add);
    }
    public  int add(int a ,int b) {
        return a+b;
    }
}
输出结果:
3
  • 值传递和引用传递
//值传递
public class Dem04 {
    public static void main(String[] args) {
        int a =1;
        System.out.println(a);
        Dem04.change(a);
        System.out.println(a);
    }
    //返回值为空
    public static void change(int a){
        a=10;
    }
}
输出结果:
1
1
//引用传递:对象的本质还是值传递
public class Dem05 {
    public static void main(String[] args) {
     Person person = new Person();
        System.out.println(person.name);
     Dem05.change(person);
        System.out.println(person.name);
    }
    public static void change(Person person){
        person.name="胡桃";
    }
}
//定义了一Person类,有一个属性:name
class Person{
        String name;
}
输出结果:
null
胡桃
  • this关键字
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值