一、Object-Oriented-Programming面向对象编程
-
- 1.构造器
- 2.封装(高内聚,低耦合)
- 3.继承,方法重写,多态 instanceof关键字和类型转换(父类与子类的转换)
- 4.static关键字
- 5.抽象类(abstract) 约束。
- 6.接口
- 7.内部类
- 8.Exception(异常)
- 9.包装类(基本类型的引用类)
- 10.整数缓冲区
- 11.String
- 12.StringBuffer可变长字符串jdk1.0就提供了,运行效率相对StringBuilder慢,线程安全(StringBuilder jdk1.5提供,运行效率快,线程不安全)
- 13.bigDecimal
- 14.Date类、Calendar类、SimpleDateFormat类 System类
1.构造器
1.构造器:用来初始化值 是默认的无参构造;
2. 使用有参构造的时候必须显示的定义无参构造
3. 使用new 关键字,本质是在调用构造器
package com.f.www.object.oopDemo01;
public class Person {
//一个类即使什么都不写,它也会存在一个方法-默认的无参构造
//显示的定义构造器
String name="qinjiang";
int age;
private int money=10_0000_0000;
//实例化初始值
//使用new 关键字,本质是在调用构造器
//用来初始化值
public int getMoney(){
return money;
}
public void setMoney(int money){
this.money=money;
}
public Person(){
};
//this.name="qinjiang";
public Person (String name){
this.name=name;
}
public void print(){
System.out.println(name);
}
public void say(){
System.out.println("hi");
}
//1.和类明相同
//没有返回值
//定义有参构造后,如果想使用无参构造,显示定义一个无参的构造
//alt+insert,快捷键
}
/*
1.类与对象
类是一个模板
2.对象是通过引用来操作的: 栈——>堆
3.属性:字段Field 成员变量
默认初始化
数字:0 0.0
char:u0000
boolean:false
引用:null
4。修饰符 属性类型 属性名 =属性值
5. 必须使用new 关键字创造对象,构造器Person xiaoming =new Person();
对象的属性 xiaoming.name
对象的方法 xiaoming.sleep
6.类:
静态的属性
动态的方法
=------------------------
package com.f.www.object.oopDemo01;
public class Application {
public static void main(String[] args) {
Person person=new Person( "w");
System.out.println(person.name);
}
}
*/
2.封装(高内聚,低耦合)
1.private:定义私有属性关键字
2.get/set 提供public的get和set方法获取
3.alt+insert 自动生成get/set方法
package com.f.www.object.oopDemo01;
public class fengZhuangStudent {
//属性私有
private String name;
private int id;
private char sex;
//public的get/set方法
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
}
/*
属性私有 get/set
1.提高程序的安全性
2.隐藏代码的实现细节
3,统一接口
4.内部代码可提高程序的可维护性
package com.f.www.object.oopDemo01;
public class Application {
public static void main(String[] args) {
fengZhuangStudent student=new fengZhuangStudent();
student.setName("xiaoming");
System.out.println(student.getName());
}
}
*/
3.继承,方法重写,多态 instanceof关键字和类型转换(父类与子类的转换)
关键字:exrends意为扩展;
Object类,所以类都继承这个类
Super:超类
this:当前类
关键字: instanceof 判断是否有父子类关系
package com.f.www.object.oopDemo01;
import java.util.Objects;
public class JCStudent extends Person {
private String name1="QINGJIANG";
public void print(){
System.out.println(name);
}
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
@Override
public String toString() {
return "JCStudent{" +
"name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
//判断是否为同一个引用
if (this == o) return true;
//判断是否为同一个类型
if (o == null || getClass() != o.getClass()) return false;
// if (o instanceof JCStudent){}
JCStudent student = (JCStudent) o;
return Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
//gc();
@Override
protected void finalize() throws Throwable {
System.out.println("回收了"+
name1+" ");
}
}
//JAVA只有单继承,继承是类与类之间的关系
/*
super注意点
1.super调用父类的构造方法,必须在构造方法中的第一个
2.super只能出现在子类的方法或构造器中
3.super和this不能同时调用构造方法
区别:
代表·的对象不同
this:本身调用者这个对象
super:代表父类对象的引用
前提条件
this:没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法
this();本类的构造
super();父类的构造
public static void main(String[] args) {
JCStudent student=new JCStudent();
student.say();
System.out.println(student.getMoney());
student.test("1");
*/
package com.f.www.object.oopDemo01;
public class methodsToRewrite {
/*
重写:需要继承关系,子类重写父类的方法 重写只针对非静态方法。静态方法没有重写一说。
方法名必须相同
参数列表必须相同
修饰符:范围可扩大,不能缩小 public>Protected>Default>private
抛出的异常:范围可以缩小,但不能扩大;classNotFoundException-->Exception(大)
A a=new A();
a.text();
//父类的引用指向了子类;
//Override 重写
B b=new A();
a.text();
Studnet S1=new Student();//Student 能调用的方法都是自己类里面的或是继承父类的
Person S2=new Student();//父类型,可以指向子类,但是不能调用子类独有的方法
Object S3=new Student();
//对象能执行哪些方法,主要看对象左边的类型,和右边大的·关系不大
((Student)S2).eat();//子类重写了父类的方法,执行子类的方法
多态:
1.多态是方法的多态,属性没有多态
2.父类和子类有联系,类型转换异常 ClassCastException
3.存在条件:继承关系,子类重写父类方法,父类指向子类对象
instanceof 判断是否存在父子关系
((类型转换) 引用类型).方法名();
((Student)S2).eat();
A a=new A();
a.text();
//父类的引用指向了子类;
//Override 重写
B b=new A();
a.text();
//System.out.println(X instanceof Y);//编译能不能通过
Object object= new JCStudent();
System.out.println(object instanceof JCStudent);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("---------------------");
Person person= new JCStudent();
System.out.println(person instanceof JCStudent);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(obj instanceof String);
JCStudent student=new JCStudent();
System.out.println(student instanceof JCStudent);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher);
// System.out.println(student instanceof String);
父 子类型转换
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转换,强制转换
4.方便方法的调用,减少重复的代码
5.不能使用多态的情况{
static方法从属于类,不属于实例
final 常量
private 私有的
}
*/
}
4.static关键字
package com.f.www.object.oopDemo01;
import static java.lang.Math.*;//静态导入包
//static
public class staticTuozhan {
private static int age;//静态变量
private double score;//非静态的变量
{
//匿名代码块(在构造器之前)(用于赋初始值)
System.out.println("匿名代码块(在构造器之前)");
}
static {
//静态代码块(从属于类,只执行一次)
System.out.println("静态代码块(从属于类,只执行一次)");
}
public staticTuozhan() {
System.out.println("构造方法");
}
public void run(){
go();//非静态方法可以调用静态方法和本身方法
run();
}
public static void go() {
// run();//静态方法不可以调用非静态方法
}
public static void main(String[] args) {
staticTuozhan s=new staticTuozhan();
go();
System.out.println();
System.out.println(staticTuozhan.age);
// System.out.println(staticTuozhan.score);
System.out.println(s.age);
System.out.println(s.score);
System.out.println(random());//random()随机数