• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

青携纸笔携香

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Java第十课---什么是面向对象与方法回顾

面向对象

 

对于描述复杂的事物,为了从宏观上把握、从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是,具体到微观操作,仍然需要面向过程的思路去处理。

 

面向对象的本质就是:以类的方式组织代码,以对象的组织(封装)数据。

 

三大特性

  • 封装

  • 继承

  • 多态

回顾方法

package com.oop;
​
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){
       if (a==b){
           return 0;
      }
       return a>b?a:b;
  }
​
   //数组下标越界异常
   public void readFile(String file)throws IOException{
​
  }
}
​

 

package com.oop;
​
//学生类
public class Student {
   //静态方法
   public static void sayOne(){
       System.out.println("静态方法说话了");
  }
   //非静态方法
   public void sayTwo(){
       System.out.println("非静态方法说话了");
  }
}
​
​
​
package com.oop;
​
public class Demo02 {
   public static void main(String[] args) {
       //静态方法static
       Student.sayOne();
       //非静态方法
       //实例化这个类 对象名=对象值;
       Student student=new Student();
       student.sayTwo();
​
  }
​
   //和类一起加载的
   public static void a(){
       b();
  }
   //类实例化之后才存在
   public static void b(){
​
  }
​
}
​

 

package com.oop;
​
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;
​
//引用传递:对象,本质还是值传递
public class Demo05 {
   public static void main(String[] args) {
       Person person=new Person();
​
       System.out.println(person.name);//null
​
       Demo05.change(person);
       System.out.println(person.name);//王海峰
  }
   public static void change(Person person){
      person.name="王海峰";
  }
}
​
//定义了一个person类,有一个属性:name
class Person{
   String name;
}
​
package com.oop;
​
//值传递
public class Demo04 {
   public static void main(String[] args) {
       int a=1;
       System.out.println(a);//1
       Demo04.change(a);
       System.out.println(a);//1
  }
​
   public static void change(int a){
       a=10;
  }
}

posted on 2022-03-04 21:13  青携纸笔携香  阅读(29)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3