Day10

package com.oop;

import java.io.IOException;

//Demo1 类
public class Demo1 {
//main
public static void main(String[] args) {

}
/*
修饰符 返回值类型 方法名(.....) {
方法体
return 返回值
}
*/
//return 代表结束方法,返回一个结果
public String sayHello(){
return "HelloWorld!";
}
public int max(int a,int b){
return a>b?a:b;//三元运算符 如果a>b则返回a;否则返回b
}
//数组下标越界:Arrayindexoutofbounds
//异常
public void readFile(String flie)throws IOException{

}
}




package com.oop;

public class Demo2 {
public static void main(String[] args) {
// 静态方法可以通过类名直接调用方法 有static
//Student.say();
//非静态方法要通过实例化这个类,才能调用这个类的方法 new关键字 没有static

//对象类型 对象名=对象值
Student studnet=new Student();
studnet.say();
}
//和类一起加载的(有static)
public static void a(){
// b();
}
//对象创建之后才存在 也就是类实例化之后(没有static)
public void b(){

}
}


package com.oop;

public class Demo3 {
public static void main(String[] args) {
//实际参数与形式参数的类型要对应
// new Demo3().add(5,6);//非静态
int add= Demo3.add(1,2);//静态
System.out.println(add);
}
//
public static int add(int a,int b){
return a+b;
}
}



package com.oop;
//值传递
public class Demo4 {
public static void main(String[] args) {
int a=1;
System.out.println(a);
Demo4.change(a);
System.out.println(a);
}
//返回值为空
public static void change(int a){
a=10;
}
}

package com.oop;
//引用传递:传递一个对象,本质还是值传递
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);//lisa
}
public static void change(Person person){
//Person是一个对象:指向---->Person person= new Person();这是一个具体的人,可以改变属性
person.name="lisa";
}
}
//定义了一个Person类,有一个属性:name
class Person{
String name;//默认为null
}

package com.oop;
//学生类
public class Student {
//非静态方法
public void say(){
System.out.println("学生说话了");
}
}
posted @ 2022-04-21 22:40  1号小白学编程  阅读(67)  评论(0编辑  收藏  举报