类与对象的创建
package opp.Demo01;
public class Demo01 {
public static void main(String[] args) {
Perosn perosn = new Perosn();
System.out.println(perosn.name);//null
Demo01.change(perosn);
System.out.println(perosn.name);
}
public static void change(Perosn perosn) {
perosn.name = "zhan";
//perosn 是一个对象:指向的 ---->Perosn perosn = new Perosn();这是一个具体的人
//可以改变属性
}
}
class Perosn{
String name;//null
}
构造器解析
package opp.Demo02;
public class Student {
String name;
int age;
//用来new关键字,本质是在调用构造器
//用来初始化值
public Student() {
}
//有参构造:一但定义了有参构造,无参构造就必须显示定义
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student( int age) {
this.age = age;
}
}
/*
public static void main(String[] args) {
Student student = new Student("zhan",23);
System.out.println(student.name);
}
*/
/*
构造器 :
1.和类名相同,没有返回值
作用:
1.new 本质在调用构造方法
2.初始化对象的值
注意点:
1.定义有参构造之后如果想使用无参构造,显示的定义一个无参构造
Alt + Insert
*/
数据的封装
package opp.Demo03;
public class Student {
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;
}
public int getId() {
return id;
}
public void setId(int id) {
if (id>=0&&id<=100){
this.id = id;
}else {
this.id = 1;
}
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
/*
package opp;
import opp.Demo03.Student;
/*
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护增加了
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.setName("zhan");
System.out.println(student.getName());
student.setId(123);//不合法的
System.out.println(student.getId());
}
}
*/
继承
package opp.Demo04;
//在Java中,所有的类,都默认直接或者间接继承Object
//Person 人 :父类
public class Person/*extends Object*/{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package opp.Demo04;
public class Student extends Person{
}
package opp.Demo04;
public class Teacher {
}
super和this
package opp.Demo04;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.Text();
student.Text1();
}
}
/*
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super 必须只能出现在子类的方法或者构造方法中!
3.super和this不能同时调用构造方法!
Vs this:
this :代表的对象不同
super: 只能在继承条件才可以使用
构造方法
this();本类的构造
super();父类的构造
*/
package opp.Demo04;
import javax.naming.Name;
import java.rmi.Naming;
public class Student extends Person{
public String name = "heng";
public Student() {
//隐藏代码:调用父类的无参构造
super("zhanhengyan");//调用父类的构造器必须在子类的第一行
System.out.println("Student无参执行了");
}
public void print(){
System.out.println("Student");
}
public void Text1(){
print();
this.print();
super.print();
} public void Text(){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
}
package opp.Demo04;
//在Java中,所有的类,都默认直接或者间接继承Object
//Person 人 :父类
public class Person/*extends Object*/{
public Person(String name) {
String Name = name;
System.out.println(name);
}
public String name = "zhan";
public void print(){
System.out.println("Person");
}
}
方法的重写:
package opp.Demo05;
public class Application {
//静态的方法和非静态的方法区别很大!
//静态方法 : //方法的调用只和左边,定义的数据类型有关
//非静态 : 重写
public static void main(String[] args) {
A a = new A();
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
a.test();
b.test(); //B
}
}
/*
重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大: public>Protected>Default>private
4.抛出的异常:范围,可以被缩小,但是不能扩大:ClassNotFoundException-->Exception(大)
重写,子类的方法和父类必须一致
为什莫需要重写
1.父类的功能,子类不一定需要,或者不一定满足!
Alt + Insert:override;
*/
package opp.Demo05;
public class A extends B {
public void test() {
System.out.println("a");
}
}
package opp.Demo05;
public class B {
public void test(){
System.out.println("b");
}
}
多态:
package opp.Demo06;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父亲的秦俑指向子类
//Student 能够调用的方法都是自己的或者继承父类的!
Student s1 = new Student();
//person 父类型,可以指向了类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大
s2.run();
s1.run();
((Student)s2).eat();//子类重写了父类的方法,执行了类的方法
}
}
/*
多态的注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常!ClassCastException!
3.多态的存在条件:继承关系,方法需要重写,父类的引用指向子类对象 Father f1 = new Son();
不能够重写的类型:
1.static 方法,属于类,它不属于实例
2.final 常量;
3.private 方法;
*/
package opp.Demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
package opp.Demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("sun");
}
public void eat(){
System.out.println("eat");
}
}
instanceof和类型转换
package opp.Demo07;
import java.util.Objects;
public class Application {
public static void main(String[] args) {
//类型之间的转换:父 子
//高 --> 低
Person person = new Student();
//Student 将这个对象转换为Student类型,我们就能使用Student这个方法了!
((Student)person).go();
}
}
/*
1.父类引用指向了类的对象
2.把子类转换为父类,向上转型;
3.把父类转换为子类,向下转型;强行转换
4.方便方法的调用,减少重复的代码!简介
封装、继承、多态
*/
package opp.Demo07;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
/*
//Object > String
//Object > Person > Teacher
//Object > Person > Student
Object object = new Student();
//System.out.println(x instanceof y);//能不能编译通过
System.out.println(object instanceof Student);//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 Student();
System.out.println(person instanceof Student);//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(person instanceof String);//编译报错
System.out.println("================================");
Student student = new Student();
System.out.println(student instanceof Student);//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); //编译报错
*/
package opp.Demo07;
public class Person {
public void run(){
System.out.println("run");
}
}
package opp.Demo07;
public class Teacher extends Person{
}
static
package opp.Demo08;
public class Person {
{
//2.匿名代码块,可以用来赋初始值;
System.out.println("匿名代码块");
}
static {
//1.静态代码块,先执行,只执行一次
System.out.println("静态代码块");
}
public Person(){
System.out.println("构造方法");
}
//3
public static void main(String[] args) {
Person personp1 = new Person();
System.out.println("===========================");
Person personp2 = new Person();
}
}
package opp.Demo08;
public class Student {
private static int age;//静态的变量
private double score;//非静态的变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
Student student = new Student();
System.out.println(student.age);
System.out.println(Student.age);
System.out.println(student.score);
student.run();
go();
}
}
package opp.Demo08;
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
抽象类
package opp.Demo09;
//abstract 抽象类: 类 extends: 单继承~ (接口可以多继承)
public abstract class Action {
//约束~有人帮我们实现~
//abstract,抽象方法,只有方法名字,没有方法的实现!
public abstract void doSomething();
/*
抽象类的特点
1.不能new 这个抽象类,只能靠子类去实现它:约束!
2.抽象类中可以写普通的方法~
3.抽象方法必须在抽象类中
抽象的抽象:约束
4.抽象类存在的意义:
公有属性的抽象 抽象出来~ 提高开发效率
*/
}
package opp.Demo09;
//抽象类的所有方法,继承了他的子类,都必须要实现他的方法~
public class A extends Action{
@Override
public void doSomething() {
}
}
interface
package opp.Demo10;
//interface 定义的关键字 , 接口都需要实现类
public interface UserService {
//常量 ~ public static final
//接口中的所有定义都是抽象的 public abstract
int AGE = 99;
void add(String name);
void dlete(String name);
void update(String name);
void query(String name);
}
/*
接口的作用:
1.约束
2.定义一些方法,让不同的人实现~ 10 ---> 1
3.public abstract
4.public static final
5.接口不能被实例化~,接口中没有构造函数~
6.implements可以实现多个接口
7.必须要重写接口中的方法~
*/
package opp.Demo10;
//抽象类 : extend~
//类 可以实现接口 implements 接口
//实现了接口的类,就需要重写接口的方法~
public class UserServiceImpl implements UserService,TimeService{
@Override
public void add(String name) {
}
@Override
public void dlete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
@Override
public void run(String name) {
}
}
package opp.Demo10;
public interface TimeService {
void run(String name);
}
内部类
=============================================================================
成员内部类
package opp.Demo11;
public class Outer {
private int ID = 10;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获取外部类的私有属性~
public void getID(){
System.out.println(ID);
}
}
}
package opp.Demo11;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
outer.out();
//通过这个外部类来实例化内部类~
Outer.Inner inner = outer.new Inner();
inner.in();
inner.getID();
}
}
=============================================================================
局部内部类: 写在方法里面
package opp.Demo11;
public class Outer {
public void method(){
//局部内部类
class Inner{
public void in(){
}
}
}
}
============================================================================
匿名内部类:
package opp.Demo11;
public class Test {
//没有名字初始化类,不用将实例保存到变量中~
public static void main(String[] args) {
new Apple().eat();
new UserService(){
@Override
public void in() {
}
};
}
}
class Apple{
public void eat(){
System.out.println("1");
}
}
interface UserService {
public void in();
}