java学习笔记31

java面向对象

回顾方法的定义

修饰符

返回类型

package oop;
//类
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;//如果a>b则结果是a否则结果为b   三元运算符

  }
   
   
}

break 和return的区别

跳出switch语句 结束循环

switch(expression){

case value:

//语句

break;//可选

执行return代表这个方法已经结束

 

 

方法名:注意规范 驼峰命名 见名知意

参数列表 (参数类型 参数名) ...可变长参数

异常抛出

 

回顾方法的调用

静态方法 static

非静态方法

package oop;                                                 package oop;
                                                             //学生类
public class Demo02 {                                        public class student {
   public static void main(String[] args) {                 //静态方法
       student.say();                                            public static void say(){
  }                                                                 System.out.println("好好学习");
                                                          }
                                                        }
}


      好好学习                                                      
package oop;

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

}
package oop;
//学生类
public class student {
   //非静态方法
   public void say(){
       System.out.println("好好学习");
  }

}
好好学习
package oop;

public class Demo02 {
  public static void main(String[] args) {
      //实例化这个类 new
      //对象类型 对象名 =对象值
      student student = new student();
      student.say();
  }
  //static和类一起加载
  public static void a(){
      b();
  }
  //类实例化之后才存在
  public void b(){
       
  }

}
一个存在一个还不存在所有报错

 

形参和实参

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;

  }
}
3
public class Demo03 {
   public static void main(String[] args) {
       new Demo03().add(1,2);
       System.out.println(new Demo03().add(1,2));
  }

   //形参
   public int add(int a,int b){
       return a+b;

  }
}
3

值传递和引用传递

package oop;
//值传递
public class Demo04 {
   public static void main(String[] args) {
       int a = 1;
       System.out.println(a);
       Demo04.change(a);
       System.out.println(a);
  }
   public static void change(int a){
       a = 10;
  }
}
1
1
package oop;
//引用传递 对象 本质还是值传递
public class Demo05 {
   public static void main(String[] args) {
       perosn perosn = new perosn();
       System.out.println(perosn.name);//null
       Demo05.change(perosn);
       System.out.println(perosn.name);//呼呼

  }
   public static void change(perosn perosn){
       //perosn是一个对象:指向的 ------->perosn perosn = new perosn();这是一个具体的人,可以改变属性
       perosn.name = "呼呼";
       
  }
}

//定义了一个perosn类,有一个属性:name
class perosn{
   String name;//null
}
null
呼呼

this关键字

posted @ 2022-09-28 15:15  新人用户  阅读(26)  评论(0)    收藏  举报