面向对象(实例二)
package oop.Dem02;
public class Person {
//一个类即使什么都不写,它也会存在一个方法
String name;
public Person(){}
//实例化初始值关键字
//1. 使用new关键字,本质是在调用构造器
//2. 用来初始化值
//public Person(){
// this.name = "xiaoming";
//}
//有参构造
public Person(String name){
this.name = name;
}
}
// alt insert
/*
//一个项目只存在一个main方法
public class Application {
public static void main(String[] args) {
//new 实例化了一个对象
Person person = new Person("xy");
System.out.println(person.name); //xiaoming
}
}
构造器:
1. 和类名相同
2. 没有返回值
作用:
1,new 本质在调用构造方法
2. 初始化对象的值
注意点
1. 定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造
*/
/*
*/
package oop.Dem02;
//学生类
public class Student {
//属性 :字段
String name; //null
int age; //0
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
/*
//类 :抽象的,实例化
//类实例化后会返回一个自已的对象
//student对象就是一个Student类的具体实例
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name = "小明";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
*/
package oop.Dem02;
public class Application {
public static void main(String[] args) {
}
}