面向对象(Object-Oriented)
面向对象三大特性
类:一种数据类型,引用数据类型,自定义的一种类型,用变量表示属性,用方法表示行为
对象:具体存在的事务,符合类的定义特征.
类的定义个数:
class 类名[]
如何创建对象
类名 对象名 = new 类名();
demo1:创建一个学生类定义姓名,性别,分数,定义一个方法study添加一个自我介绍的方法。
// StudentDemo
package obj;
public class Student {
String name;
int age;
double score;
// 定义方法
public void teach() {
System.out.println("姓名:" + this.name);
}
}
// TestDemo
package obj;
public class StudentDemo {
public static void main(String[] args) {
Student std = new Student();
std.name = "小明";
std.age = 12;
std.score = 80;
System.out.println(std);// obj.Student@4c203ea1
System.out.println(std.name);// 小明
std.teach();// 姓名:小明
}
}
package obj;
public class Student {
// 成员变量
String name;
int age;
double score;
// 静态变量
static int a;
// 成员方法
public void teach() {
System.out.println("姓名:" + this.name);
}
// 静态方法
public static void test(int x, int y) {
int abc = 100;//局部变量
}
// 构造方法
public Student(String name , int age) {
this.name = name;
this.age = age;
}
}
// 解析
当我们创建一个类时候,首先会在堆空间方法去创建class区和static区。class区保存定义的类,static保存静态方法。当我们对类进行初始化,class区将成员变量初始化并拷贝到堆内存地址中,初始化变量指向类方法内定义成员变量的地址。
封装前需要使用成员变量:private. 此为权限修饰符,当然权限修饰符还有:public,protected,default,private。
而private表示私有的,它可以修饰方法和变量,被private修饰符方法和变量,只能在本类种访问:
提供一套访问的修饰方法get和set方法。
demo
// Teacher类
package func;
public class Teacher {
private String name;
private int age;
private int gender;
private double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void show() {
System.out.println("name:" + name + "age:"+ age+ "gender:" + gender + "salary:" + salary);
}
}
// 调用封装Teacher类
package func;
public class TeacherDemo {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setName("张三");
t.setAge(18);
t.setGender(1);
t.setSalary(1235.3);
t.show();
}
}
package func;
public class Mobile {
private String brand;
private String type;
private double price;
private String color;
public void setBrand(String brand) {
this.brand = brand;
}
public void setType(String type) {
this.type = type;
}
public void setPrice(double price) {
this.price = price;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
public String getType() {
return type;
}
public double getPrice() {
return price;
}
public String getColor() {
return color;
}
public void show() {
System.out.println("brand:"+brand+"color:"+color+"type:"+type+"price:"+price);
}
}
package func;
public class MobileDemo {
public static void main(String[] args) {
Mobile m1 = new Mobile();
m1.setBrand("华为");
m1.setColor("红色");
m1.setType("5G");
m1.setPrice(2399.5);
Mobile m2 = new Mobile();
m2.setBrand("苹果");
m2.setColor("黄色");
m2.setType("X");
m2.setPrice(4399.5);
Mobile[] ms = new Mobile[] {m1, m2};
for (int i=0;i<ms.length;i++) {
ms[i].show();
}
}
}
package func;
public class Rect {
private double length;
private double width;
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
public double periMeter() {
return 2*(this.length+this.width);
}
public double area() {
return this.length*this.width;
}
}
package func;
public class RectDemo {
public static void main(String[] args) {
Rect r = new Rect();
r.setLength(10);
r.setWidth(5);
double perimeter = r.periMeter();
double area = r.area();
System.out.println(perimeter);// 30.0
System.out.println(area);// 50.0
}
}
package gouzao;
public class Teacher {
private String name;
private int age;
private double salary;
public Teacher(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public void show() {
System.out.println("name:"+name+"age:"+age+"salary:"+salary);
}
}
package gouzao;
public class TeacherDemo {
public static void main(String[] args) {
Teacher t = new Teacher("Tom",20,25002.3);
t.show();
}
}
原文:https://www.cnblogs.com/xujunkai/p/13702293.html