Java的四大面向对象编程概念,成功定级腾讯T3-2

@Override public void stop()

{

System.out.println( “Honda::Stop” );

System.out.println(

“Mechanism to stop the car using break” );

}

}

public class Main {

public static void main(String args[])

{

Car obj

= new Honda(); // Car object =>contents of Honda

obj.stop(); // call the method

}

}

支柱2:封装

封装是将代码和数据封装到一个单元中的过程。

真实的例子:

由几种药物混合而成的胶囊。这些药物是给最终用户的隐藏数据。

为了在java中实现封装,请按照以下建议执行某些步骤:

  • 将变量声明为私有变量

  • 声明SETETER和gettersto设置并获取变量值

注意:在java中封装很少有缺点,如下所示:

1.对数据的控制:我们可以在setter方法中写入逻辑,以避免存储整数的负值。这样我们就可以控制数据。

2.数据隐藏:数据成员是私有的,因此其他类无法访问数据成员。

3.易于测试:单元测试对于封装类很容易。

例子:

// A Java class which is a fully encapsulated class.

public class Car

{

// private variable

private String name;

// getter method for name

public String getName()

{

return name;

}

// setter method for name

public void setName(String name)

{

this .name = name

}

}

// Java class to test the encapsulated class.

public class Test

{

public static void main(String[] args)

{

// creating instance of the encapsulated class

Car car

= new Car();

// setting value in the name member

car.setName( “Honda” );

// getting value of the name member

System.out.println(car.getName());

}

}

支柱3:继承

继承是一个类在Java中从另一个类继承属性和方法的过程。当我们有是-a对象之间的关系。Java中的继承是通过以下方式实现的延展关键词。

现实生活的例子:

地球和火星继承了超级太阳系,太阳系继承了银河系。因此,银河系是一流的太阳系,地球和火星的超级等级。

在提出代码之前,让我们用一个通用示例来讨论继承在java应用程序中的使用。因此,考虑一个扩展Exception类的示例,以创建一个特定于应用程序的异常类,该类包含更多信息,如错误代码。例如,NullPointerException。

Java中有5种不同类型的继承,如下所示:

1.**单一继承:**类B使用扩展关键字继承B类

2.**多级继承:**C类继承B类,B类使用EXTEND关键字继承A类

3.**等级继承:**类B和C使用EXTEND关键字按层次顺序继承A类

4.**多重继承:**C类继承了A和B类,这里A和B都是超类,C只是一个子类。Java不支持多重继承,但我们可以使用接口实现。

5.**混合继承:**类D继承类B,C类继承A。这里,类D继承了两个超类,因此Java也不支持混合继承。

例子:

// super class

class Car {

// the Car class have one field

public String wheelStatus;

public int noOfWheels;

// the Car class has one constructor

public Car(String wheelStatus, int noOfWheels)

{

this .wheelStatus = wheelStatus;

this .noOfWheels = noOfWheels;

}

// the Car class has three methods

public void applyBrake()

{

wheelStatus = “Stop” System.out.println(

“Stop the car using break” );

}

// toString() method to print info of Car

public String toString()

{

return ( "No of wheels in car " + noOfWheels + “\n”

  • "status of the wheels " + wheelStatus);

}

}

// sub class

class Honda extends Car {

// the Honda subclass adds one more field

public Boolean alloyWheel;

// the Honda subclass has one constructor

public Honda(String wheelStatus, int noOfWheels,

Boolean alloyWheel)

{

// invoking super-class(Car) constructor

super (wheelStatus, noOfWheels);

alloyWheel = alloyWheel;

}

// the Honda subclass adds one more method

public void setAlloyWheel(Boolean alloyWheel)

{

alloyWheel = alloyWheel;

}

// overriding toString() method of Car to print more

// info

@Override public String toString()

{

ret

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

urn ( super .toString() + "\nCar alloy wheel "

  • alloyWheel);

}

}

// driver class

public class Main {

public static void main(String args[])

{

Honda honda = new Honda( 3 , 100 , 25 );

System.out.println(honda.toString());

}

}

支柱4:java的多态性

多态是以多种方式执行许多事情的能力。“多态”一词来自两个不同的希腊语单词–多态和变形。“Poly”的意思是很多,“变形”的意思是形式。所以多态意味着很多形式。在继承的情况下也可以存在多态性。根据实际实现,这些函数的行为不同。

现实生活的例子:

送货人将物品交付给用户。如果是邮递员,他会送信的。如果是送餐的男孩,他会把食物交给用户。像这样,多态性为传递函数实现了不同的方式。

有两种类型的多态性,如下所示:

1.静态或编译时多态性

2.动态或运行时多态性

静态或编译时多态性当编译器能够确定实际函数时,它被称为编译时多态编译时多态可以通过方法超载在爪哇。当类中的不同函数具有相同的名称但有不同的签名时,就称为方法重载。方法签名包含名称和方法参数。因此,重载方法有不同的参数。参数在数字或参数类型上可能有所不同。

例1:静态多态性

public class Car{

public void speed() {

}

public void speed(String accelerator) {

}

public int speed(String accelerator, int speedUp) {

return carSpeed;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值