此为笔者个人笔记,如有错误还请指出
public class TestTemplate {
//main模板生成main方法
public static void main(String[] args) {
//sout模板生成System.out.println();
System.out.println("sout yyds");
//fori模板生成for循环
// for (int i = 0; i < ; i++) {
//
// }
}
}
//我们可以将setter写在构造器中,这样仍然能达到封装的效果
public Person(String name, int age, int salary){
setName(name);
setAge(age);
setSalary(salary);
}
class 子类 extends 父类{
//...
}
public Pupil(String name, int age, double score) {
//
setScore(score);
this.name = name;
this.age=age;
System.out.println("调用子类有参构造器");
}
public Pupil(String name, int age, double score) {
//
super(name, age, score);
System.out.println("调用子类有参构造器");
}
public class Run {
public static void main(String[] args) {
Son son = new Son();
System.out.println(son.name);
System.out.println(son.salary);
System.out.println(son.hobby);
}
}
class Grandpa{
String name="爷爷";
String hobby="玩";
}
class Father extends Grandpa{
String name="爸爸";
double salary=10000.1;
}
class Son extends Father{
String name="儿子";
int age=14;
}
//输出:
儿子
10000.1
玩
public class Exercise01 {
public static void main(String[] args) {
B b = new B();
}
}
class A {
A() {
System.out.println("a");
}
A(String name) {
System.out.println("a name");
}
}
class B extends A {
B() {
this("abc");
System.out.println("b");
}
B(String name) {
//默认有super()
System.out.println("b name");
}
}
//运行结果:
a
b name
b
//注意这里输出的 a 是由 B(String name) 调用的,B()使用了this(),因此不能再使用隐藏的super();
//预期结果:
我是A类
hahaha我是B的有参构造
我是c类的有参构造
我是c类的无参构造
public class Animal {
public void cry(){
System.out.println("动物在叫");
}
}
public class Dog extends Animal {
@Override
public void cry(){
System.out.println("小狗在叫");
}
}
public class first {
public static void main(String[] args) {
Person mxy = new Person("mxy", 19);
Student yll = new Student("yll", 18, "2021212121", 85.01);
mxy.say();
yll.say();
}
}
public class Person {
private String name;
private int age;
public Person(){
setName("无名氏");
setAge(0);
}
public Person(String name, int age){
setName(name);
setAge(age);
}
public void say(){
System.out.println("你好!我叫"+name+",今年"+age+"岁了。");
}
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 class Student extends Person {
private String id;
private double score;
public Student(){
setId("");
setScore(0);
}
public Student(String name, int age, String id, double score){
super(name, age);
setId(id);
setScore(score);
}
@Override
public void say(){
System.out.println("大家好!我叫"+getName()+",学号是"+id+",今年"
+getAge()+"岁了,成绩是"+score+"分");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
Animal animal = new Dog();
Animal animal = new Animal();
animal = new Cat();
animal.cry();
public class Exercise02 {
public static void main(String[] args) {
Sub s = new Sub();
System.out.println(s.cnt);
s.say();
Base b = s;
System.out.println(b == s);
System.out.println(b.cnt);
b.say();
}
}
class Base{
int cnt = 10;
public void say(){
System.out.println(this.cnt);
}
}
class Sub extends Base{
int cnt = 20;
public void say(){
System.out.println(this.cnt);
}
}
//向下转型后属性看编译类型,方法看运行类型,输出为
20
20
true
10
20
public class DynamicBinding {
public static void main(String[] args) {
A a = new B();
System.out.println(a.getI()); //10
System.out.println(a.sum()); //20
System.out.println(a.sum1()); //30
}
}
class A{
public int i = 20;
public int sum(){
return getI() + 10;
}
public int getI(){
return i;
}
public int sum1(){
return i + 10;
}
}
class B extends A{
public int i = 10;
@Override
public int getI() {
return i;
}
}
public class Person {
private String name;
private int age;
public Person(String name, int age){
setName(name);
setAge(age);
}
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 String say(){
return name+"\t"+age;
}
}
public class Student extends Person {
private double score;
public Student(String name, int age, double score){
super(name, age);
setScore(score);
}
public void Study(){
System.out.println(super.getName()+"正在学习");
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String say() {
return super.say()+"\tscore:"+getScore();
}
}
public class Teacher extends Person {
private double salary;
public Teacher(String name, int age, double salary) {
super(name, age);
this.salary = salary;
}
public void teach(){
System.out.println(super.getName()+"老师正在授课");
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String say() {
return super.say()+"\tsalary:"+getSalary();
}
}
public class PolyArr {
public static void main(String[] args) {
//要将1Person 2Student 2Teacher对象放在同一数组
Person[] persons = new Person[5];
persons[0] = new Person("1", 1);
persons[1] = new Student("2",2,100);
persons[2] = new Student("3",3,60);
persons[3] = new Teacher("4",4,10000);
persons[4] = new Teacher("5",5,20000);
for (int i = 0; i < persons.length ; i++) {
System.out.println(persons[i].say());
//假如这里想要调用Student类中的特有方法Study怎么办?
//可以判断运行类型+向下转型
if(persons[i] instanceof Student){
((Student)persons[i]).Study();
}else if(persons[i] instanceof Teacher){
((Teacher)persons[i]).teach();
}
}
}
}
//输出
1 1
2 2 score:100.0
2正在学习
3 3 score:60.0
3正在学习
4 4 salary:10000.0
4老师正在授课
5 5 salary:20000.0
5老师正在授课
public class Employee {
private String name;
private double salary;
public double getAnnual(){
return 12 * getSalary();
}
public Employee(String name, double salary) {
setName(name);
setSalary(salary);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public class Manager extends Employee {
private double bonus;
public void setBonus(double bonus) {
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public Manager(String name, double salary, double bonus) {
super(name, salary);
setBonus(bonus);
}
@Override
public double getAnnual() {
return super.getAnnual() + getBonus();
}
public void manage(){
System.out.println("经理 "+super.getName()+"正在研究管理方法");
}
}
public class OrdEmployee extends Employee {
public OrdEmployee(String name, double salary) {
super(name, salary);
}
@Override
public double getAnnual() {
return super.getAnnual();
}
public void Work(){
System.out.println("员工 "+super.getName()+"正在工作");
}
}
public class PolyParameter {
public static void main(String[] args) {
Employee e1 = new Manager("e1",10000,13.14);
Employee e2 = new OrdEmployee("e2", 7000);
Employee e3 = new Employee("e3", 6000);
showEmpAnnual(e1);
showEmpAnnual(e2);
showEmpAnnual(e3);
testWork(e1);
testWork(e2);
testWork(e3);
}
public static void showEmpAnnual(Employee e){
if(e instanceof OrdEmployee){
System.out.println(((OrdEmployee)e).getAnnual());//动态绑定机制
}else if(e instanceof Manager){
System.out.println(((Manager)e).getAnnual());//动态绑定机制
}else{
System.out.println("类型错误!");
}
}
public static void testWork(Employee e){
if(e instanceof OrdEmployee){
((OrdEmployee)e).Work();//动态绑定机制
}else if(e instanceof Manager){
((Manager)e).manage();//动态绑定机制
}else{
System.out.println("类型错误!");
}
}
}
//输出:
120013.14
84000.0
类型错误!
经理 e1正在研究管理方法
员工 e2正在工作
类型错误!
//注意这段代码输出为true
int a = 65;
float f = 65f;
System.out.println(a == f);
//String的equals方法源码:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
//Object类的euqals方法源码:
public boolean equals(Object obj) {
return (this == obj);
}
//每个引用类型都会重写equals方法
public class Person { //extends Object
private String name;
private int age;
private char gender;
//重写Object的equals方法
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj instanceof Person){
//向下转型
Person p = (Person)obj;
return this.age == p.age && this.name == p.name && this.gender == p.gender;
}else{
return false;
}
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public Person(String name, int age, char gender){
setName(name);
setAge(age);
setGender(gender);
}
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 String say(){
return name+"\t"+age;
}
}
public class EqualsTest {
public static void main(String[] args) {
Person p1 = new Person("jetty", 12,'男');
Person p2 = new Person("jetty", 12,'男');
//如果不重写equals方法,比较会对比地址,从而输出false
System.out.println(p1.equals(p2));
}
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public class EqualsTest {
public static void main(String[] args) {
Person p1 = new Person("jetty", 12,'男');
System.out.println(p1.toString());
}
}
//输出:
com.mat.object_.equals.Person@1b6d3586
//若不toString,直接System.out.println(p1); 会默认调用toString()
public class Test {
public static void main(String[] args) {
Cat kitty = new Cat("meow");
kitty = null;
System.gc();
System.out.println("由于没有引用指向此Cat对象," +
"系统调用了finalize方法将它回收了");
}
}
class Cat{
public String name;
public Cat(String name) {
this.name = name;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
//如果不主动调用System.gc();这里会没输出,由于JVM运用了特殊的算法来决定垃圾何时回收,这里后面讲到JVM的时候再讲
System.out.println("调用了重写的finalize方法");
}
}
//输出:
由于没有引用指向此Cat对象,系统调用了finalize方法将它回收了
调用了重写的finalize方法