1.Object类是所有类的父类
2.java中的每个类都可以使用Object中定义的方法
equals()
toString()
3.final
(1)修饰类表示不允许被继承
(2)修饰方法表示不允许被子类重写,可以被整除继承使用
(3)修饰变量表示不允许被修改,修饰引用类型变量时:初始化之后不能在指向另外对象,但指向的对象的内容是可变的
(4)可配合static使用
4.注解
可以声明在包、类、属性、方法、局部变量、方法参数等的前面,用来对这些元素进行说明、注释。
按照运行机制分:源码注解、编译时注解、运行时注解
按照来源分:来自JDK的注解、来自第三方的注解、我们自己定义的注解
5.方法重写和方法重载
方法重写:
在满足继承关系的子类中
方法名、参数个数、顺序、类型与父类相同
返回值类型与父类兼容
访问修饰符的限定范围大于等于父类方法
方法重载:
在同一个类中
方法名相同
参数个数、顺序、类型不同
返回值类型、访问修饰符任意
6.案例:
package com.immoc.animal;
/*final class:该类没有子类 public final class / final public classs
*final 方法:该方法不允许被子类重写,但是可以正常被子类继承使用
*final 方法内的局部变量:只要在具体被使用之前进行赋值即可,一旦赋值不允许被修改
* 类中成员属性:赋值过程:1、定义直接初始化 2、构造方法 3、构造代码块
*/
public class Animal {
/*private:只允许在本类中进行访问
*public:允许在任意位置访问
*protected:允许在当前类、同包子类/非子类、跨包子类调用,跨包非子类不允许
*默认:允许在当前类、同包子类/非子类调用,跨包子类/非子类不允许调用
* */
private String name="妮妮";//昵称
protected int month=2;//月份
String species="动物";//品种
public static final int temp=12;
public static int st2=23;
private static int st1=22;
static{
System.out.println("我是父类的静态代码块");
}
{
// temp=12;
System.out.println("我是父类的构造代码块");
}
//父类的构造不允许被继承、不允许被重写,但是会影响子类对象的实例化
public Animal(){
System.out.println("我是父类的无参构造方法");
}
public Animal (String name,int month){
this.name=name;
this.month=month;
System.out.println("我是父类的带参构造方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
//吃东西
public void eat(){
System.out.println(this.getName()+"在吃东西");
}
public void eat(String name){
final int temp;//方法内的局部变量
// temp=12;
System.out.println(name+"在吃东西");
temp=12;
System.out.println(temp);
final Animal animal=new Animal("凡凡",1);
// animal=new Animal();
animal.month=12;
animal.name="豆豆";
}
public boolean equals(Object obj){
// if(obj==null)
// return false;
Animal temp=(Animal)obj;
//此处的equals调用的是String类中的equals方法,是比较字符串的实际大小
if(this.getName().equals(temp.getName())&&(this.getMonth()==temp.getMonth()))
return true;
else
return false;
}
public boolean euqals(Animal obj){
// if(obj==null)
// return false;
if(this.getName().equals(obj.getName())&&(this.getMonth()==obj.getMonth()))
return true;
else
return false;
}
public String toString(){
return "昵称:"+this.getName()+"年龄:"+this.getMonth();
}
public Animal create(){
return new Animal();
}
}
package com.immoc.animal;
public class Dog extends Animal {
private String sex;//性别
public Dog(){
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//睡觉的方法
public void sleep(){
super.eat();//调用的哪个eat();
super.species="犬科";
System.out.println(this.getName()+"现在"+this.getMonth()+"个月大,它正在睡觉~~");
}
/*
* 方法重载:
* 1、在同一个类中
* 2、方法名相同,参数列表不通(参数的顺序、个数、类型)
* 3、方法返回值、访问修饰符任意
* 4、与方法的参数名无关
*
* 方法重写
* 1、有继承关系的子类中
* 2、方法名相同,参数列表相同(参数顺序、个数、类型),方法返回值可以允许是子类类型
* 3、访问修饰符,访问范围需要大于等于父类的访问范围
* 4、与方法的参数名无关
* */
// private String sleep(String name){
// return "";
// }
// public void sleep(String name,int month){
//
// }
// public void sleep(int month,String name){
//
// }
// public void sleep(int name,String month){
//
// }
//子类重写父类吃东西的方法
// public void eat(){
// System.out.println(this.getName()+"最近没有食欲~~");
// }
// public void eat(String month){
// System.out.println(month+"最近没有食欲~~");
// }
// @Override
// public void eat(String name) {
// // TODO Auto-generated method stub
// super.eat(name);
// }
@Override
public void eat() {
// TODO Auto-generated method stub
super.eat();
}
@Override
public Dog create() {
// TODO Auto-generated method stub
return new Dog();
}
}
package com.immoc.animal;
public class Cat extends Animal{
private double weight=11.24;//体重
// public int temp=300;
public static int st3=44;
static{
System.out.println("我是子类的静态代码块");
}
{
System.out.println("我是子类的构造代码块");
}
public Cat(){
// Animal temp=new Animal();
// temp.name;
// this.temp=12;
// this.month=23;
// this.species="";
System.out.println("我是子类的无参构造方法");
}
public Cat(String name,int month){
/* 子类构造默认调用父类无参构造方法
* 可以通过super()调用父类允许被访问的其他构造方法
* super()必须放在子类构造方法有效代码第一行
* */
super(name, month);
// this();
System.out.println("我是子类的带参构造方法");
}
public static void say(){
// this.weight=20;
// super.name="aa";
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//跑动的方法
public void run(){
eat();
System.out.println(this.getName()+"是一只"+this.getSpecies()+",它在快乐的奔跑");
}
}
package com.imooc.test;
import com.immoc.animal.Animal;
import com.immoc.animal.Cat;
import com.immoc.animal.Dog;
public class Test {
public static void main(String[] args){
// Cat one=new Cat();
// one.setName("花花");
// one.setSpecies("中华田园猫");
// one.eat();
// one.run();
// System.out.println(one.temp);
// System.out.println("==========================");
Dog two=new Dog();
two.setName("妞妞");
two.setMonth(1);
two.eat();
// two.sleep();
// System.out.println("==========================");
// two.eat("凡凡");
// System.out.println("==========================");
// Animal three=new Animal();
// three.month=2;
// three.species="";
// three.name;
// three.run();
// three.sleep();
}
}