什么是面向对象
以类的方式组织代码,以对象的方式组织(封装)数据。
回顾方法的定义
值传递
package oop.demo01;
public class demo01 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);//a=1
num(a);
System.out.println(a);//a=1
}
public static void num(int a){
a = 10;
}
}
引用传递:对象(本质还是值传递)
package oop.demo01;
public class demo02 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);//null
demo02.cheng(person);
System.out.println(person.name);//zhangf
}
public static void cheng(Person person){
person.name = "zhangf";
}
}
class Person {
String name;//null
}
类与对象的创建
package oop.demo02;
public class student {
String name;
int age;
public void study(){
System.out.println(this.name + "在学习...");
}
}
package oop.demo02;
public class Application {
public static void main(String[] args) {
student xh = new student();
student xm = new student();
xh.name = "小红";
xh.age = 13;
xh.study();
System.out.println(xh.name);
System.out.println(xh.age);
}
}
构造器详解
构造方法 快捷键 command + N
创建对象内存分析
封装详解
package oop.demo03;
public class student {
private String name;
private int id;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package oop.demo03;
public class Appaciaction {
public static void main(String[] args) {
student s1 = new student();
s1.setName("qinf");
System.out.println(s1.getName());
}
}
继承
package oop.demo04;
public class Person {
public String name = "winlo";
private int age = 13;
public void say(){
System.out.println("讲了一句话...");
}
}
package oop.demo04;
public class Student extends Person{
}
package oop.demo04;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.name);
s1.say();
}
}
super详解
package oop.demo04;
public class Person {
protected String name = "person1";
}
package oop.demo04;
public class Student extends Person{
public String name = "student1";
public void test(String name){
String nn = name;
System.out.println(nn);
System.out.println(this.name);
System.out.println(super.name);
}
}
package oop.demo04;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.test("winl");
}
}
package oop.demo04;
public class Person {
protected void print(){
System.out.println("Person");
}
}
package oop.demo04;
public class Student extends Person{
public void print(){
System.out.println("Student");
}
public void test1(){
print();
this.print();
super.print();
}
}
package oop.demo04;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.test1();
}
}
在new一个对象的时候会在调用自己的构造器的同时也会去调用父类的构造器;
显示调用父类构造器的话【super()】,要把它放在第一行;
调用this()也要放在第一行。
如果父类的无参构造没了,那么子类的无参构造也不能去生成了。
方法重写
什么是多态
package oop.demo06;
public class Person {
public void run(){
System.out.println("Person run...");
}
}
package oop.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("Student run");
}
}
package oop.demo06;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.run();
Person s2 = new Student();
s2.run();
}
}
instanceof和类型转换
有句话是:编译看左👈🏻,运行看右👉🏻
static 关键字详解
package oop.demo07;
public class Application {
static {
System.out.println("静态代码块");
}
{
System.out.println("匿名代码块");
}
public Application() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Application application = new Application();
System.out.println("============================");
Application a1 = new Application();
}
}
抽象类
继承抽象类的子类必须实现它的所有方法
接口
接口中所有定义的都是 public abstract
接口中定义的属性都是常量,也没人太会这么玩儿~
实现了接口的类就需要重写接口里的所有方法
接口可以多继承
package oop.demo09;
public interface UserSerice {
void add(String id);
void delete(String id);
}
package oop.demo09;
public interface TimeTiler {
void timer();
}
package oop.demo09;
public class UserSericeImpl implements UserSerice,TimeTiler{
@Override
public void add(String id) {
}
@Override
public void delete(String id) {
}
@Override
public void timer() {
}
}
内部类
package oop.demo10;
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 oop.demo10;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//outer.out();
Outer.inner inner = outer.new inner();
inner.in();
}
}
内部类获取外部类的私有属性
一个Java类中可以有多个class类,但只能有一个public class