什么是面向对象
回顾方法和加深
import java.io.IOException;
//Demon01 类
public class Demo01 {
//main 方法
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(....){
return 返回值;
}
*/
public String sayHello(){
return "Hello,world!";
}
public void hello(){
return;
}
public int max(int a,int b){
return a>b ? a : b; //如果a>b 结果是a 否则是b 三元运算符
}
//数组下标越界
public void readFile(String file) throws IOException{
}
}
public class Demo02 {
public static void main(String[] args) {
//实例化这个类 new
//对象类型 对象名 = 对象值;
Student student = new Student();
student.say();
}
//和类一起加载的
public static void a(){
//b();
}
//实例化后才存在的
public void b(){
}
}
public class Demo03 {
public static void main(String[] args) {
//实参和形参类型要对应
int add = Demo03.add(1, 2);
}
public static int add(int a,int b){
return a+b;
}
}
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;
}
}
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); //fengzi
}
public static void change(Person person){
//person是一个对象,指向一个类Person person = new Person();;这是一个而具体的人,可以改变属性
person.name = "fengzi";
}
}
//定义了一个person的类,有一个属性name
class Person{
String name;
}
package com.oop.Demon02;
//学生类
public class Student {
//属性:字段
String name;//null
int age;//0
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
/*
Person--->身高,体重,年龄,国籍,...
学程序好的原因是对世界进行更好的建模!---不要宅
音乐 旅游 出国
*/
package com.oop.Demon02;
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
//类,是抽象的,需要实例化
//类实例化后会返回一个自己的对象名字
//student对象就是一个student类的具体实例
Student xiaoming = new Student();
Student xh = new Student();
xiaoming.name = "小明";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
xh.name = "小红";
xh.age=3;
System.out.println(xh.name);
System.out.println(xh.age);
}
}
原文:https://www.cnblogs.com/ariesmark/p/15101221.html