目录
一 类的继承
关于继承我们可以举一个列子,比如电脑可以打游戏,听音乐,上网聊天,看视频。而平板电脑就继承了这些功能,同时它也有一些其他的特性,比如拍照等等。
在java中,一个类继承另一个类需要使用关键字extends,关键字extends的使用方法如下:
class Child extends Parent{}
因为java只支持单继承,即一个类只能有一个父类,所以类似下面的代码是错误的:
class Child extends Parent1,Parent2{}
以电脑和平板为列:
创建父类:
public class Computer {//父类:电脑
String screen="液晶显示屏";
void starup() {
System.out.println("电脑正在开机请稍等!");
}
}
创建子类:
public class Pad extends Computer{
}
创建其他类:
public class Demo {
public static void main(String[] args) {
Computer computer=new Computer();//父类
Pad ipad=new Pad();
System.out.println(computer.screen);//父类
computer.starup();
System.out.println(ipad.screen);//父类
ipad.starup();
}
}
运行后的结果:
显然可以发现子类继承了父类的特性。
假如不加extends:
就会发现这里报错。
我们假如pad的特性:
修改子类:
public class Pad extends Computer {
String battery="5000毫安";
void open5G()
{
System.out.println("打开5G网络!");
}
}
再次输出:
public class Demo {
public static void main(String[] args) {
Computer computer=new Computer();//父类
Pad ipad=new Pad();
System.out.println(computer.screen);//父类
computer.starup();
System.out.println(ipad.screen);//子类
ipad.starup();
ipad.open5G();
System.out.println(ipad.battery);
}
}
子类的特性可以输出。
父类不可以调用子类不然就会报错:
接下来我们思考打开图片的方法,电脑是用鼠标点击,平板用手指触摸,但他们都用showpictur方法。
public class Computer {//父类:电脑
String screen="液晶显示屏";
void starup() {
System.out.println("电脑正在开机请稍等!");
}
void showPicture() {
System.out.println("电脑:点击打开。");
}
}
public class Pad extends Computer {
String battery="5000毫安";
void open5G()
{
System.out.println("打开5G网络!");
}
}
public class Demo {
public static void main(String[] args) {
Computer computer=new Computer();//父类
Pad ipad=new Pad();
System.out.println("图片打开方式:");
computer.showPicture();
ipad.showPicture();
}
}
运行结果:
很明显平板现实的就不对,因此我们要在子类修改。修改如下:
public class Pad extends Computer {
String battery="5000毫安";
void open5G()
{
System.out.println("打开5G网络!");
}
void showPicture() {
System.out.println("平板:用手指点击.");
}
}
这就是方法重写,将父类的方法覆盖掉了。
1.1super关键词
既然覆盖掉了我们如何在调用父类方法呢?
为了解决这个问题Java提供了super关键词。
【语法】super.方法名
比如:
public class Pad extends Computer{
public void do(){
super.do();
}
}
在举个列子:
public class Pad extends Computer {
String battery="5000毫安";
void open5G()
{
System.out.println("打开5G网络!");
}
void showPicture() {
System.out.println("平板:用手指点击.");
}
String sayHello(){
return ("欢迎使用"+"平板电脑") ;
}
}
public class Computer {//父类:电脑
String screen="液晶显示屏";
void starup() {
System.out.println("电脑正在开机请稍等!");
}
void showPicture() {
System.out.println("电脑:用鼠标点击.");
}
String sayHello(){
return ("欢迎使用") ;
}
}
public class Demo {
public static void main(String[] args) {
Computer computer=new Computer();//父类
Pad ipad=new Pad();
System.out.println(computer.sayHello());
System.out.println(ipad.sayHello());
}
}
我们发现如果要改变成英文那么父类也要修改,子类也要修该,十分麻烦。
如果不用:
明显pad没变。
修改后就可以了。
super的三种用法:
1.2【两点注意】
1.Java中一个类只能有一个父类。
如果我们想要使用可以使用多重继承就可以解决:
2.子类不仅会覆盖父类的方法,还会覆盖父类的属性。
比如:
public class Demo {
public static void main(String[] args) {
child a=new child("Tom");
System.out.println(a.name);
}
}
class Parent1 {
String name;
public Parent1(String name) {
this.name=name;
}
}
class child extends Parent1{
String name="Jack";//虽然与父类的属性同名,但任然属于子类独有的特性。
public child(String name) {
super(name);
}
}
这里子类的Jack将Tom覆盖,输出的结果为:
二 Object类
在学习使用class关键字定义类时,就应用到了继承原理,因为在Java中所有的类都直接或间接继承了java.lang.object类。object类时比较特殊的类,它是所有类的父亲,是Java类层中最高层类。所有的类都是Object的子类。
2.1Object常用的三个方法
1.getclass()方法
getclass()方法是Object类定义的方法,它会返回对象执行时的Class实列,然后使用此实例调用getname()方法可以取得类的名称。语法如下:
getclass().getname();
举例:
public class Demo {
public static void main(String[] args) {
Object[] arr=new Object[3];
arr[0]=new Object();
arr[1]=new String("字符串");
arr[2]=new Demo();
for(Object x:arr) {
System.out.println(x.getClass());
}
}
}
2.toString()方法
toString ()方法的功能是将一个对象返回为字符串形式,它会返回一个String实列。
public class Demo {
public static void main(String[] args) {
Object[] arr=new Object[4];
arr[0]=new Object();
arr[1]=new String("字符串");
arr[2]=new Integer(10);
arr[3]=new Demo();
for(Object obj:arr) {
System.out.println(obj.toString());
}
}
public String toString() {
return("我是梁志超他奶奶");
}
}
3.epuals()方法
3. equals()方法
在Java语言中,有两种比较对象的方式,分别为“==”运算符与equals)方法。两者的区别在“==”比较的是两个对象引用内存地址是否相等,而equals方法比较的是两个对象的实际内容。
实列:判断是不是一个人。
public class People {
String name;
String Id;
public boolean equals(Object obj) {
People p=(People)obj;
boolean b1=this.name.equals(p.name);
boolean b2=this.Id.equals(p.Id);
return b1&&b2;
}
}//创建people类
public class Demo {
public static void main(String[] args) {
People p1=new People();
People p2=new People();
People p3=new People();
People p4=new People();
p1.name="小明";
p1.Id="123";
p2.name="小红";
p2.Id="123";
p3.name="小明";
p3.Id="456";
p4.name="小明";
p4.Id="123";
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println(p1.equals(p4));
}
}
【训练】打印水果类价格
public class apple {
String name;
String color;
String weight;
String price;
public apple(String name,String color,String weight,String price) {
this.name=name;
this.color=color;
this.weight=weight;
this.price=price;
}
public String toString() {
return(color+"的苹果被称为"+"\""+name+"\""+",每"+weight+"克"+price+"元,"+"买了2500克"+"\""+name+"\""+"需要支付多少元?");
}
public static void main(String[] args) {
apple a=new apple("糖心富士","红色","500","4.98");
System.out.println(a.toString());
}
}