面向对象的三个特征
封装
public class People {
private String age;//年龄
private String sex;//性别
private String height;//高度
private String name;//姓名
public People(){
System.out.println("我是一个人");
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printInfo(){
System.out.println("名字:"+name+",年龄:"+age);
}
}
//main
public static void main(String[] args) {
People people = new People();
people.setAge("20");
people.setName("张三");
people.printInfo();
}
//打印结果
我是一个人
名字:张三,年龄:20
继承
public class yellow extends People{
private String skinColor; //皮肤颜色
public String getSkinColor() {
return skinColor;
}
public void setSkinColor(String skinColor) {
this.skinColor = skinColor;
}
// super(),调用无参父类构造方法
public yellow(){
super();
}
//super().printInfo() 调用父类 printInfo()方法
public void printInfo(){
super.printInfo();
System.out.println("皮肤:"+skinColor);
}
}
//main
public static void main(String[] args) {
People people = new People();
people.setAge("20");
people.setName("张三");
people.printInfo();
yellow yellow = new yellow();
yellow.setAge("15");
yellow.setName("李四");
yellow.setSkinColor("黄皮肤");
yellow.printInfo();
}
//结果
我是一个人
名字:张三,年龄:20
我是一个人
名字:李四,年龄:15
皮肤:黄皮肤
多态
同一个行为不同表现形式
//People.class,加上一个方法
public void power(){
System.out.println("我会打飞机");
}
//yellow.class,加上一个方法
public void power(){
System.out.println("我会打手枪");
}
//main
public static void main(String[] args) {
//父类
People people = new People();
people.setAge("20");
people.setName("张三");
people.printInfo();
//子类
yellow yellow = new yellow();
yellow.setAge("15");
yellow.setName("李四");
yellow.setSkinColor("黄皮肤");
yellow.printInfo();
//同一个方法,不同行为
people.power();
yellow.power();
//父类引用指向子类对象
People people1 = new yellow();
people1.power();
}
//打印结果
我是一个人
名字:张三,年龄:20
我是一个人
名字:李四,年龄:15
皮肤:黄皮肤
我会打飞机
我会打手枪
我是一个人
我会打手枪
super 有参/与无参
//父类
public People(){
System.out.println("我是一个人");
}
public People(String change_a){
System.out.println(change_a);
}
//子类
// super(),调用无参父类构造方法
public yellow(){
// super();
super("子类传给父类");
}
//main
public static void main(String[] args) {
//无参
People people = new People();
//有参
People people1 = new People("变量a");
//子类
yellow yellow = new yellow();
}
//打印结果
我是一个人
变量a
子类传给父类
基本类型(8种)
//整型:byte short int long
byte b1 = 12; //[-128, 127] byte,即字节,由8位的二进制组成
short s1;
int i1;
long l1 =66666666666666L;//声明long型变量 必须以 "1" 或者 "L"结尾
// 浮点型 定义float类型变量时 变量要以 "f" 或"F" 结尾
float f1 = 123.4f;
double d1 = 123.456;
//字符型 char
char c1 = 'c';
//布尔型:boolean true or false
boolean boo = true;
equals与==的区别
https://www.cnblogs.com/limeiky/p/4831992.html //比较详细
静态变量和实例变量
public class JavaTypeClass {
public static void main(String[] args) {
System.out.println(testtwo.test);
tesThree tesThree = new tesThree();
System.out.println(tesThree.testthree);
}
}
class testtwo{
static public String test = "test";
}
class tesThree{
public String testthree = "test3";
}
重载和重写
重载Overload表示的是同一个类中可以有多个相同名称的方法,但这些方法的参数列表不同,即就是参数参数或参数类型不同。
重写Override表示子类中的方法可以与父类中的方法名称和参数完全相同,通过子类创建的对象来调用这个方法。