概念
使用
案例
public class PersonText {
public static void main(String[] args) {
Person person = new Person();
person.name = "dq";
person.age = 11;
person.eat("番茄炒蛋");
}
}
class Person {
/**
* 姓名
*/
String name;
/**
* 年龄
*/
Integer age;
/**
* 方法-吃饭
*/
void eat(String lunch) {
System.out.println(age + "岁的" + name + "吃" + lunch);
}
}
对象的内存解析1
案例1
public class PersonText {
public static void main(String[] args) {
Person person1 = new Person();
person1.name = "dq";
person1.age = 11;
person1.eat("番茄炒蛋");
Person person2 = new Person();
person2.name = "dq2";
person2.age = 112;
person2.eat("番茄炒蛋2");
Person person3=person1;
person3.eat("3");
}
}
class Person {
/**
* 姓名
*/
String name;
/**
* 年龄
*/
Integer age;
/**
* 方法-吃饭
*/
void eat(String lunch) {
System.out.println(age + "岁的" + name + "吃" + lunch);
}
}
对象的内存解析2
案例
成员变量和局部变量
案例
详细
方法概念
分类
说明
return后面不写东西
方法注意点
案例1
package lesson.l10_oop;
/**
* Illustration
*
* @author DengQing
* @version 1.0
* @datetime 2022/7/1 9:02
* @function
*/
public class PersonDemo {
public static void main(String[] args) {
PersonObject person1 = new PersonObject();
person1.name="tt";
person1.age=30;
person1.sex=1;
person1.study();
person1.addAge(2);
person1.showAge();
PersonObject person2 = new PersonObject();
person2.name="oo";
person2.age=18;
person2.sex=2;
person2.study();
person2.addAge(2);
person2.showAge();
}
}
class PersonObject {
String name;
int age;
/**
*1是男性
* 2是女性
*/
int sex;
public void study() {
System.out.println("studying");
}
public void showAge() {
System.out.println("age:" + age);
}
public int addAge(int i) {
this.age += i;
return this.age;
}
}
案例2
package lesson.l10_oop;
/**
* Illustration
*
* @author DengQing
* @version 1.0
* @datetime 2022/7/1 9:16
* @function 计算圆的面积
*/
public class CircleText {
public static void main(String[] args) {
Circle circle = new Circle();
circle.r = 0.5;
System.out.println("圆的面积为" + circle.countArea());
}
}
class Circle {
double r;
public double countArea() {
return Math.pow(r, 2) * 3.14;
}
}
案例3
package lesson.l10_oop;
/**
* Illustration
*
* @author DengQing
* @version 1.0
* @datetime 2022/7/1 9:46
* @function
*/
public class Method {
public static void main(String[] args) {
Method m=new Method();
System.out.println("矩形的面积为:"+m.method(10,8));
}
public int method(int m,int n){
// 宽
for (int i = 0; i < n; i++) {
// 长
for (int j = 0; j < m; j++) {
System.out.print("*\t");
}
System.out.println();
}
return m*n;
}
}