当前位置: 首页 > news >正文

【Java】还有人不懂继承?25 个 Case 包教包会

还有人不懂继承?25 个 Case 包教包会

  • 1.Implement single inheritance
  • 2.Implement multilevel inheritance
  • 3.Implement hierarchical inheritance
  • 4.Override a base class method into a derived class
  • 5.Demonstrate the protected access specifier
  • 6.Create an Student class by inheriting Person class
  • 7.Call the method with the same name using super keyword
  • 8.Call a superclass constructor from sub/child class
  • 9.Method Overloading and Inheritance
  • 10.Final Class and Inheritance
  • 11.Static Methods and Inheritance
  • 12.Multiple Inheritance using Interfaces
  • 13.Method Overriding with Exception Handling
  • 14.Accessing Superclass Members
  • 15.Method Overriding with Covariant Return Types
  • 16.Constructor Overriding and Superclass Initialization
  • 17.Demonstrates method overriding
  • 18.Demonstrates the use of the protected access specifier
  • 19.Demonstrates accessing superclass members using the super keyword
  • 20.Demonstrates the use of a final class in inheritance
  • 21.Demonstrates the use of abstract classes in inheritance
  • 22.Demonstrate the order of superclass and subclass initialization
  • 23.Demonstrate dynamic method dispatch
  • 24.Demonstrate interface inheritance
  • 25.Diamond problem in multiple inheritance

Java 的三大特性:封装、继承、多态。本篇博客将重点通过一些案例讲解 继承 这个概念。

1.Implement single inheritance

在这里插入图片描述

public class SingleInheritance // Main class
{public static void main(String[] args){        Dog dog = new Dog("Buddy");	 // Create an instance of Dog        dog.eat();	// Call methods from Animal class        dog.bark();	// Call methods from Dog class}
}class Animal // Parent class (Superclass)
{protected String name;public Animal(String name){this.name = name;}public void eat(){System.out.println(name + " is Eating");}
}class Dog extends Animal // Child class (Subclass) inheriting from Animal
{public Dog(String name){super(name);}public void bark(){System.out.println(name + " is Barking");}
}
Buddy is Eating
Buddy is Barking

super() / super(parameters) 必须出现在子类构造方法的第一行,这是显式调用父类构造方法的唯一方式。因为在子类中调用父类构造方法的名字会引起一个语法错误。构造方法可以用来构造一个类的实例,不像属性和方法,父类的构造方法是不被子类继承的,它们只能从子类的构造方法中用 super 调用。

2.Implement multilevel inheritance

在这里插入图片描述

public class MultilevelInheritance	// Main class
{public static void main(String[] args){        Puppy puppy = new Puppy("Buddy");	// Create an instance of Puppy        puppy.eat();	// Call methods from Animal class        puppy.bark();	// Call methods from Dog class        puppy.play();	// Call methods from Puppy class}
}class Animal	// Grandparent class
{protected String name;public Animal(String name){this.name = name;}public void eat(){System.out.println(name + " is Eating");}
}class Dog extends Animal	// Parent class inheriting from Animal
{public Dog(String name){super(name);}public void bark(){System.out.println(name + " is Barking");}
}class Puppy extends Dog		// Child class inheriting from Dog
{public Puppy(String name){super(name);}public void play(){System.out.println(name + " is Playing");}
}
Buddy is Eating
Buddy is Barking
Buddy is Playing

构造方法链constructor chaining):在任何情况下,构造一个类的实例时,将会调用沿着继承链的所有父类的构造方法。当构造一个子类的对象时,子类构造方法会在完成自己的任务之前,首先调用它的父类的构造方法。如果父类继承自其他类,那么父类构造方法又会在完成自己的任务之前,调用它的父类的构造方法。这个过程持续到沿着这个继承体系机构的最后一个构造方法被调用为止。

3.Implement hierarchical inheritance

在这里插入图片描述

public class HierarchicalInheritance	// Main class
{public static void main(String[] args){        Car car = new Car();	// Create objects of child classesMotorcycle motorcycle = new Motorcycle();car.display();	// Calling methods of parent classmotorcycle.display();car.displayCar();	// Calling methods of child classesmotorcycle.displayMotorcycle();}
}class Vehicle	// Parent class
{void display(){System.out.println("This is a Vehicle");}
}class Car extends Vehicle	// Child class 1
{void displayCar(){System.out.println("This is a Car");}
}class Motorcycle extends Vehicle	// Child class 2
{void displayMotorcycle(){System.out.println("This is a Motorcycle");}
}
This is a Vehicle
This is a Vehicle
This is a Car
This is a Motorcycle

4.Override a base class method into a derived class

这个 Java 程序演示了方法覆盖,这是面向对象编程中的一个概念,即子类为父类中已定义的方法提供特定实现。在这个程序中,有两个类: Vehicle 和 Car。汽车类中的显示方法覆盖了车辆类中的显示方法。下面是对程序的解释:

public class OverrideMethod		// Main class
{public static void main(String[] args){        Car car = new Car();	// Create an object of the derived class       car.display();		 // Call the overridden method}
}class Vehicle	// Base class
{void display(){System.out.println("This is a Vehicle");}
}class Car extends Vehicle	// Derived class
{@Overridevoid display(){System.out.println("This is a Car");}
}
This is a Car

5.Demonstrate the protected access specifier

public class AccessSpecifier	// Main class
{public static void main(String[] args){        Car car = new Car();   // Create an object of the child class        car.setBrand("Toyota");	 // Set the brand using the public method        car.displayBrand();	  // Access the protected member and method}
}class Vehicle	// Parent class
{protected String brand;protected void displayBrand(){System.out.println("Brand : " + brand);}
}class Car extends Vehicle	// Child class
{public void setBrand(String brand){this.brand = brand;}
}
Brand : Toyota

6.Create an Student class by inheriting Person class

此 Java 程序说明了继承的概念,特别是单一继承。继承是面向对象编程的一个基本概念,它允许一个类(子类或派生类)继承另一个类(超类或基类)的属性和方法。在本程序中,有两个类: Person 和 Student,其中 Student 继承自 Person。下面是对该程序的解释:

public class InheritingExample
{public static void main(String[] args){Student stu = new Student(1000, "Ram Kumar", 25, 88.96f);stu.printStudentDetails();}
}class Person
{String name;int age;Person(int age, String name){this.name = name;this.age = age;}
}class Student extends Person
{int id;float per;Student(int idn, String name, int age, float percent){super(age, name);id = idn;per = percent;}void printStudentDetails(){System.out.println("Student ID     :  " + id);System.out.println("Student Name   :  " + name);System.out.println("Student Age    :  " + age);System.out.println("Student Percentage :  " + per);}
}
Student ID     :  1000
Student Name   :  Ram Kumar
Student Age    :  25
Student Percentage :  88.96

7.Call the method with the same name using super keyword

这个 Java 程序演示了如何使用 super 关键字从父类调用方法。在这个程序中,有三个类: Parent 类、Child1 类和 Child2 类,形成一个类层次结构。Child1 和 Child2 类继承自父类。每个子类都会覆盖显示方法,并在执行各自的特定操作前使用 super 关键字调用父类的显示方法。下面是对程序的解释:

public class SuperKeyword	// Main classs
{public static void main(String[] args){Child1 child1 = new Child1();	// Create objects of the Child classesChild2 child2 = new Child2();child1.display();	// Call the display() methodchild2.display();}
}class Parent	// Parent class
{void display(){System.out.println("Parent class display method");}
}class Child1 extends Parent	 // Child class 1
{void display(){super.display();  // Call the display() method of the parent classSystem.out.println("Child 1 class display method");}
}class Child2 extends Parent	  // Child class 2
{void display(){super.display();  // Call the display() method of the parent classSystem.out.println("Child 2 class display method");}
}
Parent class display method
Child 1 class display method
Parent class display method
Child 2 class display method

8.Call a superclass constructor from sub/child class

这个 Java 程序演示了构造函数链以及在创建子类对象时使用 super 关键字调用超类构造函数的过程。在这个程序中,有两个类: Super 和 Sub,其中 Sub 继承自 Super。下面是对该程序的解释:

public class Constructor
{public static void main(String[] args){Sub obj = new Sub();}
}class Super
{Super(){System.out.println("Super class Constructor called");}
}class Sub extends Super
{Sub(){super();System.out.println("Sub class Constructor called");}
}
Super class Constructor called
Sub class Constructor called

9.Method Overloading and Inheritance

这个 Java 程序演示了方法重载,这是面向对象编程中的一个概念,即一个类可以有多个名称相同但参数列表不同的方法。在这个程序中,有两个类: Vehicle(父类)和 Car(子类),Car 类重载了 start 方法。下面是对程序的解释:

public class MethodOverloading	// Main class
{public static void main(String[] args){Car car = new Car();car.start();  	// Accessing parent class methodcar.start(3); 	// Accessing overloaded method in child class}
}class Vehicle	// Parent class
{void start(){System.out.println("Vehicle starting ...");}
}class Car extends Vehicle	// Child class
{void start(int count){System.out.println("Starting " + count + " Cars ...");}
}
Vehicle starting ...
Starting 3 Cars ...

10.Final Class and Inheritance

在这个 Java 程序中,您定义了一个名为 FinalClass 的类和一个名为 Vehicle 的 final 类。final 类是一个不能被其他类子类或扩展的类。下面是对程序的解释:

public class FinalClass	  // Main class
{public static void main(String[] args){Vehicle vehicle = new Vehicle();vehicle.start();}
}final class Vehicle	// Final parent class
{void start(){System.out.println("Vehicle starting ...");}
}
Vehicle starting ...

11.Static Methods and Inheritance

这个 Java 程序演示了静态方法在类层次结构中的使用。在这个程序中,有两个类: Vehicle(父类)和 Car(子类)。这两个类都有一个名为 display 的静态方法。静态方法属于类而不是类的实例,它们可以使用类名本身来调用。下面是对程序的解释:

public class StaticMethods	// Main class
{public static void main(String[] args){Vehicle.display();   // Accessing static method in parent classCar.display();       // Accessing static method in child class}
}class Vehicle	// Parent class
{static void display(){System.out.println("This is a Vehicle");}
}class Car extends Vehicle	// Child class
{static void display(){System.out.println("This is a Car");}
}
This is a Vehicle
This is a Car

12.Multiple Inheritance using Interfaces

这个 Java 程序演示了接口、多接口实现和方法重载的使用。在这个程序中,有两个接口(Flyable 和 Swimmable)和一个实现了这两个接口的类(Bird)。下面是对该程序的解释:

public class Interfaces	 // Main class
{public static void main(String[] args){Bird bird = new Bird();bird.fly();bird.swim();}
}interface Flyable   // First interface
{void fly();
}interface Swimmable	 // Second interface
{void swim();
}class Bird implements Flyable, Swimmable  // Class implementing multiple interfaces
{@Overridepublic void fly(){System.out.println("Bird is Flying");}@Overridepublic void swim(){System.out.println("Bird is Swimming");}
}
Bird is Flying
Bird is Swimming

13.Method Overriding with Exception Handling

在这个 Java 程序中,您将演示方法重载和异常处理的使用。程序定义了一个父类和一个继承自父类的子类。子类重载了显示方法,并更改了可抛出的异常类型。下面是对该程序的解释:

public class MethodOverride	 // Main class
{public static void main(String[] args){Parent parent = new Child();try{parent.display();}catch (Exception e){e.printStackTrace();}}
}class Parent  // Parent class	
{void display() throws Exception{System.out.println("Parent's display method");}
}class Child extends Parent	// Child class
{@Overridevoid display() throws RuntimeException{System.out.println("Child's display method");}
}
  • Parent parent = new Child();: 这一行创建了一个子类对象,但将其赋值给父类的引用。这样做是允许的,因为 Java 支持多态性,父类的引用变量可以引用子类的对象。

在这个程序中,子类中的显示方法覆盖了父类中的显示方法,并改变了它可以抛出的异常类型。这是允许的,因为子类方法可以抛出父类方法所抛出异常的子类型。

当你调用父类对象的 display 方法时,它实际上调用了子类的 display 方法,并且 Child's display method 被打印到控制台。但是,子类中的 display 方法会抛出一个 RuntimeException,而主方法没有明确捕获该异常。因此,出现了一个未处理的异常,堆栈跟踪被打印到控制台。

Child's display method

14.Accessing Superclass Members

这个 Java 程序演示了如何使用 super 关键字从子类访问父类的成员。在这个程序中,有两个类: Parent(父类)和 Child(子类)。子类扩展了父类,并使用 super 关键字访问父类中的受保护成员。下面是对该程序的解释:

public class SuperClassMember	// Main class
{public static void main(String[] args){Child child = new Child();child.displayMessage();}
}class Parent  // Parent class
{protected String msg = "Hello World";
}class Child extends Parent	// Child class
{void displayMessage(){System.out.println(super.msg); // Accessing parent class member using super keyword}
}
Hello World

15.Method Overriding with Covariant Return Types

这个 Java 程序说明了在处理不同返回类型时方法重载和多态性的概念。在这个程序中,有两个类: Animal(父类)和 Dog(子类),Dog 类覆盖了 Animal 类的 makeSound 方法。下面是对该程序的解释:

public class ReturnTypes  // Main class
{public static void main(String[] args){Animal animal = new Dog();System.out.println(animal.makeSound());}
}class Animal   // Parent class
{String makeSound(){return "Animal Makes a Sound";}
}class Dog extends Animal  // Child class
{@OverrideString makeSound(){return "Dog Barks";}
}
Dog Barks

16.Constructor Overriding and Superclass Initialization

在这个 Java 程序中,有一个名为 Vehicle 的父类和一个名为 Car 的子类。Car 类继承自 Vehicle 类,两个类都有用于初始化属性的构造函数。该程序演示了如何初始化子类的属性,同时确保正确设置父类的属性。下面是对该程序的解释:

public class Initialization	 // Main class
{public static void main(String[] args){Car car = new Car("Ford", "Porsche");System.out.println("Brand : " + car.brand);System.out.println("Model : " + car.model);}
}class Vehicle	// Parent class
{String brand;Vehicle(String brand){this.brand = brand;}
}class Car extends Vehicle	// Child class
{String model;Car(String brand, String model){super(brand);this.model = model;}
}
Brand : Ford
Model : Porsche

17.Demonstrates method overriding

这个 Java 程序演示了类继承和方法重载。它定义了两个类,Shape(父类)和 Circle(子类),其中 Circle 继承自 Shape。程序使用 calculateArea 方法计算圆的面积,该方法在 Circle 类中被重载。下面是对程序的解释:

class Circle extends Shape
{double radius;Circle(double radius){this.radius = radius;}@Overridevoid calculateArea(){double area = Math.PI * radius * radius;System.out.println("Area of Circle : " + area);}public static void main(String[] args){Circle circle = new Circle(5.0);circle.calculateArea();}
}class Shape
{void calculateArea(){System.out.println("Area calculation not implemented");}
}
Area of Circle : 78.53981633974483

18.Demonstrates the use of the protected access specifier

这个 Java 程序演示了受保护访问修饰符和继承的使用。程序中有两个类,Person(父类)和 Student(子类)。Student 类继承自 Person 类,可以访问父类中定义的受保护方法。下面是对该程序的解释:

class Student extends Person
{void showDetails(){displayDetails(); // Protected method is accessible in the derived class}public static void main(String[] args){Student student = new Student();student.showDetails();}
}class Person
{protected void displayDetails(){System.out.println("This is a person");}
}
This is a person

19.Demonstrates accessing superclass members using the super keyword

这个 Java 程序说明了方法重载的概念,以及子类如何使用 super 关键字调用超类中的方法。在这个程序中,有两个类: Animal(父类)和 Dog(子类)。Dog 类扩展了 Animal 类,并覆盖了 displaySound 方法。下面是该程序的说明:

class Dog extends Animal
{@Overridevoid displaySound(){super.displaySound(); // Access superclass methodSystem.out.println("Dog barks");}public static void main(String[] args){Dog dog = new Dog();dog.displaySound();}
}class Animal
{void displaySound(){System.out.println("Animal makes a sound");}
}
Animal makes a sound
Dog barks

20.Demonstrates the use of a final class in inheritance

在这个 Java 程序中,您定义了一个名为 FinalClass 的 final 类,然后尝试创建一个名为 DerivedClass 的类来扩展 FinalClass。然而,这会导致编译错误,因为您不能从 final 类继承。

在类中使用 final 关键字时,表示该类不能被子类化或扩展。因此,任何从最终类(FinalClass)创建子类(如 DerivedClass)的尝试都会导致编译错误。

final class FinalClass
{// Some final class members
}// This line will result in a compilation error since FinalClass is final
class DerivedClass extends FinalClass
{// Some code here
}
Error : cannot inherit from final FinalClass

21.Demonstrates the use of abstract classes in inheritance

这个 Java 程序演示了如何使用 抽象类方法重载 来计算形状的面积。它定义了一个抽象类 Shape 和两个具体子类 Circle 和 Rectangle,这两个子类提供了计算圆形和矩形面积的具体实现。下面是对该程序的解释:

public class Main
{public static void main(String[] args){Circle circle = new Circle(5);Rectangle rectangle = new Rectangle(4, 6);System.out.println("Area of Circle : " + circle.calculateArea());System.out.println("Area of Rectangle : " + rectangle.calculateArea());}
}abstract class Shape
{abstract double calculateArea();
}class Circle extends Shape
{double radius;Circle(double r){radius = r;}@Overridedouble calculateArea(){return Math.PI * radius * radius;}
}class Rectangle extends Shape
{double length, width;Rectangle(double l, double w){length = l;width = w;}@Overridedouble calculateArea(){return length * width;}
}
Area of Circle : 78.53981633974483
Area of Rectangle : 24.0

22.Demonstrate the order of superclass and subclass initialization

在这个 Java 程序中,有两个类:Base 和 Derived,它们分别代表一个基类和一个派生类(或子类)。这些类包括实例块和构造函数,用于演示对象实例化过程中的执行顺序。下面是对该程序的解释:

public class Main
{public static void main(String[] args){Derived derived = new Derived();}
}class Base //Base Class
{{System.out.println("Base class instance block");}Base(){System.out.println("Base class constructor");}
}class Derived extends Base //Derived Class
{{System.out.println("Derived class instance block");}Derived(){System.out.println("Derived class constructor");}
}

该程序演示了在创建继承自基类的派生类对象时,实例块和构造函数的执行顺序。

Base class instance block
Base class constructor
Derived class instance block
Derived class constructor

23.Demonstrate dynamic method dispatch

这个 Java 程序说明了多态性和继承在面向对象编程中的应用。它定义了一个类的层次结构,包括一个父类 Animal 和两个子类 Dog 和 Cat。Animal 类有一个 sound 方法,子类覆盖该方法以提供各自特定的声音行为。主方法创建了一个动物对象数组,并通过调用数组中每个元素的声音方法来演示多态行为。下面是对程序的解释:

public class Main
{public static void main(String[] args){Animal[] animals = new Animal[3];animals[0] = new Dog();animals[1] = new Cat();animals[2] = new Animal();for (Animal animal : animals){animal.sound();}}
}class Animal
{void sound(){System.out.println("Animal makes a sound");}
}class Dog extends Animal
{@Overridevoid sound(){System.out.println("Dog barks");}
}class Cat extends Animal
{@Overridevoid sound(){System.out.println("Cat meows");}
}
Dog barks
Cat meows
Animal makes a sound

24.Demonstrate interface inheritance

这个 Java 程序说明了接口的使用,以及一个类如何实现多个接口以提供不同的行为集。在这个程序中,有两个接口:Drawable 和 Resizable,以及一个实现了这两个接口的名为 Shape 的类。Shape 类为这些接口中定义的方法提供了具体实现。下面是对程序的解释:

public class Main
{public static void main(String[] args){Shape shape = new Shape();shape.draw();shape.resize();}
}interface Drawable
{void draw();
}interface Resizable
{void resize();
}class Shape implements Drawable, Resizable
{@Overridepublic void draw(){System.out.println("Drawing the Shape");}@Overridepublic void resize(){System.out.println("Resizing the Shape");}
}

在这个程序中,Shape 类实现了两个接口,即 Drawable 和 Resizable,它们提供了不同的行为集。当调用 Shape 类实例的绘制和调整大小方法时,将执行已实现接口的方法。这样,Shape 类就具有了绘制和调整大小的能力,使其成为一个由接口定义了多种行为的多功能对象。

Drawing the Shape
Resizing the Shape

25.Diamond problem in multiple inheritance

在这里插入图片描述

这个 Java 程序演示了当多个接口提供了一个方法的默认实现,而一个类实现了这些接口时,如何使用 super 关键字解决菱形问题。当一个类继承自两个或两个以上的类或接口,而这些类或接口都有一个同名的方法时,就会出现菱形问题。下面是对程序的解释:

public class Main
{public static void main(String[] args){D d = new D();d.display();}
}interface A
{default void display(){System.out.println("Interface A");}
}interface B extends A
{default void display(){System.out.println("Interface B");}
}interface C extends A
{default void display(){System.out.println("Interface C");}
}class D implements B, C
{@Overridepublic void display(){B.super.display(); // Resolving the diamond problem by specifying which interface's method to callC.super.display();}
}

当在 D 类的实例上调用显示方法时,它会在控制台中打印 Interface BInterface C,正如调用 B.super.display()C.super.display() 所指定的那样。这演示了如何通过明确指定应调用哪个接口的方法来解决菱形问题。

Interface B
Interface C

相关文章:

【Java】还有人不懂继承?25 个 Case 包教包会

还有人不懂继承?25 个 Case 包教包会 1.Implement single inheritance2.Implement multilevel inheritance3.Implement hierarchical inheritance4.Override a base class method into a derived class5.Demonstrate the protected access specifier6.Create an Stu…...

Qt实现窗口失去焦点抖动功能

一、失去焦点检测 当窗口失去焦点时会发出FocusOut事件,具体实现如下: 首先给窗口安装事件过滤器: this->installEventFilter(this);然后在事件过滤器函数中判断有没有失去焦点 bool MessageDialog::eventFilter(QObject *object, QEve…...

Flink 数据源

原理 在 Flink 中,数据源(Source)是其中一个核心组件,负责从各种来源读取数据供 Flink 程序处理。 Flink 的数据源类型丰富,涵盖了从简单测试到生产环境使用的各种场景。Kafka、Socket、文件和集合是 Flink 中最常见…...

在本地电脑中如何用命令操作远程服务器上的数据库

日常做服务器维护,经常操作的2个事情,一个是备份远程服务器上的数据库到本地电脑,一个是将备份下来的数据库是恢复到本机做测试用。下面以阿里云的mysql为例,看看怎么弄。电脑是win10系统,先打开cmd命令行模式&#xf…...

uniApp子组件监听数据的变化的方法之一

props:{//用来接收外界传递过来的数据swiperList:{type:Array,default:[]}}, swiperList:是父组件传递过来的值 通过 watch 监听(在父组件中也同样可以使用,跟VUE的监听数据变化同理) watch:{//监听组件中的数据变化swiperList(ol…...

Python容器化技术的15个Docker实践

今天,我们将一起探索如何利用Docker这一强大的容器化工具,来提升你的Python项目开发、部署效率。通过一系列由浅入深的实践案例,你将学会如何将Python应用装入“小盒子”,让它在任何地方都能轻松运行。 1. Docker入门&#xff1a…...

QT天气预报项目(写在简历上)

一、ui设计 实现功能:可以搜索不同的城市进行天气的查询,并且显示未来7天内的天气,并绘制出当天的最高气温和最低气温曲线图。 学到的知识: stylesheet界面美化 Json数据解析 HTTP通信get请求 使用事件过滤器绘制温度曲线 多控件处理(利用数组) 代码整合调试能力 二…...

从零到一建设数据中台 - 数据可视化

从零到一建设数据中台(八)- 数据可视化 一、数据可视化大屏 数据可视化是借助于图形化手段,清晰有效地传达与沟通信息。 将一些业务的关键指标通过数据可视化的方式展示到一块或多块LED大屏上,以大屏为主要展示载体的数据可视化设计。 在数据可视化大屏构建过程中,为了…...

一步步实现知乎热榜采集:Scala与Sttp库的应用

背景 在大数据时代,网络爬虫技术发挥着不可或缺的作用。它不仅能够帮助我们快速地获取互联网上的信息,还能处理和分析这些数据,为我们提供深刻的洞察。知乎,作为中国领先的问答社区,汇聚了各行各业的专家和广大用户的…...

Windows和Linux系统部署Docker(2)

目录 一、Linux系统部署docker 前置环境: 1.安装需要的软件包, yum-util 提供yum-config-manager功能 2.添加阿里云 docker-ce 仓库 3.安装docker软件包 4.启动 docker并设置开机自启 5.查看版本: 二、windows系统部署docker 1.查看…...

PyCharm中快速搭建Python虚拟环境的指南

在 PyCharm 中创建一个新的 Python 虚拟环境可以帮助你为不同的项目管理不同的依赖包,避免版本冲突。以下是在 PyCharm 中创建虚拟环境的步骤: 打开或创建一个项目: 如果你还没有打开 PyCharm,首先打开它,然后选择“Open”打开一个…...

C++模板元编程

C模板元编程 为什么需要模板函数&#xff1f; 避免重复写代码 模板函数定义 使用template <class T> 或者template <typename T>其中T是可以变成任何类型调用时候T会替换成需要的类型 twice<int>会将T替换成int template <class T> T twice(T t) {re…...

Lambda表达式与函数式接口

### 泛型&#xff08;Generics&#xff09; 泛型是Java SE 5引入的一个重要特性&#xff0c;它允许在类、接口和方法中使用类型参数&#xff0c;从而提供编译时的类型安全检查和更高的重用性。java public class GenericsExample {public static <T> void printList(Li…...

Java字符串String详解

Java中的String类作为存储和操作文本数据的基本类型&#xff0c;是开发过程中最常用的类型。 String类型的声明及初始化与基本数据类型非常相似&#xff1a; String name "lcy";但是String类型是引用类型&#xff0c;有着非常丰富的处理字符串的方法。正是因为其重…...

互联网政务应用安全管理规定:使用安全连接方式访问

前几日&#xff0c;由中央网络安全和信息化委员会办公室、中央机构编制委员会办公室、工业和信息化部、公安部等4部门联合制定的《互联网政务应用安全管理规定》&#xff08;以下简称规定&#xff09;发布了&#xff0c;规定定义了互联网政务应用&#xff0c;也对互联网政务应用…...

安全测试用例及解析(Word原件,直接套用检测)

5 信息安全性测试用例 5.1 安全功能测试 5.1.1 标识和鉴别 5.1.2 访问控制 5.1.3 安全审计 5.1.4 数据完整性 5.1.5 数据保密性 5.1.6 软件容错 5.1.7 会话管理 5.1.8 安全漏洞 5.1.9 外部接口 5.1.10 抗抵赖 5.1.11 资源控制 5.2 应用安全漏洞扫描 5.2.1 应用安全漏洞扫描 5.3…...

github将默认分支main改为master

github将默认分支main改为master 1.进入github&#xff0c;点击setting 2.在setting中&#xff0c;选择Respositories&#xff0c;更新默认分支为master 3.选择要更新的项目&#xff0c;在项目中选择setting->general->切换默认分支...

java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader

问题描述&#xff1a;在maven项目中&#xff0c;给SAXReader创建实例&#xff0c;启动tomcat服务器后报异常java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader。我在pom文件中是引入了dom4j依赖得&#xff0c;但是不知道为什么在上传到web时就找不到了 解决办法&#x…...

读后感:《SQL数据分析实战》运营SQL实用手册

学习SQL&#xff0c;先有用起来&#xff0c;有了使用价值&#xff0c;之后才是去了解它的原理&#xff0c;让使用更加顺畅。 在大部分业务场景中&#xff0c;通过SQL可以快速的实现数据处理与统计。《SQL数据分析实战》区别于其他工具书&#xff0c;它并没有介绍SQL是什么&…...

建设人工智能平台,主流GPU卡选型分析

国内外主流GPU卡性能分析&#xff01;2024&#xff01; 大模型兴起助推算力需求激增 2024年&#xff0c;深度学习与人工智能技术飞速跃进&#xff0c;Transformer、GPT-3等大模型在自然语言处理、图像识别、语音合成等领域大放异彩&#xff0c;开启AI新纪元。其庞大的参数与数…...

RTSPtoWebRTC、RTSPtoWeb ( 自HTML播放):页面中预览摄像机视频,无插件的播放方式,适合局域网使用,无需流媒体服务器

文章目录 引言I 环境准备II RTSPtoWebRTC2.1 下载和编译2.2 配置config.jsonIII RTSPtoWebRTC问题优化: 使用http接口生成视频资源进行播放3.1 调用http接口生成视频资源进行播放3.2 启动关闭IV RTSPtoWeb4.1 config.json4.2 RTSPPlayersee also引言 需求: 海域感知,云台监控…...

C语言| 三个整数从小到大排序

【分析思路】 三个整数从小到大排序 这个程序的算法是&#xff1a; 先把第一个数num1跟它后面所有的数相比较&#xff0c;找出最小的&#xff0c;通过中间变量temp交换,赋给num1&#xff1b; 接着中间值num2和它后面所有的数相比较&#xff0c;找出第二小的&#xff0c;然后赋给…...

C语言基础编程题目解析:探索逻辑与算法的奥秘

C语言基础编程题目解析&#xff1a;探索逻辑与算法的奥秘 在编程的世界里&#xff0c;C语言作为一门基础且强大的编程语言&#xff0c;其题目往往涵盖了丰富的逻辑和算法知识。下面&#xff0c;我们将从四个方面、五个方面、六个方面和七个方面&#xff0c;对一系列C语言基础编…...

jmeter基础入门练习题

jmeter存在A,B两个线程组的情况下&#xff0c;默认设置下&#xff0c;运行顺序是&#xff1a;A A&#xff1a;A,B同时运行 B&#xff1a;先运行A&#xff0c;在运行B C&#xff1a;先运行A&#xff0c;等待2s运行B D:先A运行完&#xff0c;等待默认设置时间后运行B 下列说法正…...

大数据技术原理(三):HDFS 最全面的 API 操作,你值得收藏

&#xff08;实验二 熟悉常用的HDFS操作&#xff09; -------------------------------------------------------------------------------------------------------------------------------- 一、实验目的 1.理解 HDFS在 Hadoop体系结构中的角色。 HDFS是一个分布式文件系…...

Flink系列二:DataStream API中的Source,Transformation,Sink详解(^_^)

在上面篇文章中已经对flink进行了简单的介绍以及了解了Flink API 层级划分&#xff0c;这一章内容我们主要介绍DataStream API 流程图解&#xff1a; 一、DataStream API Source Flink 在流处理和批处理上的 source 大概有 4 类&#xff1a; &#xff08;1&#xff09;基于本…...

最好的电脑数据恢复软件是什么

由于硬件故障、恶意软件攻击或意外删除而丢失文件可能会造成巨大压力。数据丢失会扰乱日常运营&#xff0c;造成宝贵的业务时间和资源损失。在这些情况下&#xff0c;数据恢复软件是检索丢失或损坏数据的最简单方法。 数据恢复软件何时起作用&#xff1f; 对于 Windows 数据恢…...

机器学习模型调试学习总结

1.学习内容 模型调试方法&#xff1a;冻结部分层&#xff0c;训练剩余层 实践&#xff1a;在一个预训练的 BERT 模型上冻结部分层&#xff0c;并训练剩余的层 模型调试方法&#xff1a;线性探测&#xff08;Linear Probe&#xff09; 实践&#xff1a;在一个预训练的 BERT …...

文明互鉴促发展——2024“国际山地旅游日”主题活动在法国启幕

5月29日&#xff0c;2024“国际山地旅游日”主题活动在法国尼斯市成功举办。中国驻法国使领馆、法国文化旅游部门、地方政府、国际组织、国际山地旅游联盟会员代表、旅游机构、企业、专家、媒体等围绕“文明互鉴的山地旅游”大会主题和“气候变化与山地旅游应对之策”论坛主题展…...

【C++进阶】深入STL之string:掌握高效字符串处理的关键

&#x1f4dd;个人主页&#x1f339;&#xff1a;Eternity._ ⏩收录专栏⏪&#xff1a;C “ 登神长阶 ” &#x1f921;往期回顾&#x1f921;&#xff1a;C模板入门 &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; ❀STL之string &#x1f4d2;1. STL基本…...

一、初识Qt 之 Hello world

一、初识Qt 之 Hello world 提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 初识Qt 之 Hello world文章目录 一、Qt 简介二、Qt 获取安装三、Qt 初步使用四、Qt 之 Hello world1.新建一个项目 总结 一、Qt 简介 C &#xf…...

nginx搭建简单负载均衡demo(springboot)

目录 1 安装nignx 1.1 执行 brew install nginx 命令&#xff08;如果没安装brew可百度搜索如何安装brew下载工具。类似linux的yum命令工具&#xff09;。 1.2 安装完成会有如下提示&#xff1a;可以查看nginx的配置文件目录。 1.3 执行 brew services start nginx 命令启动…...

SpringBoot的第二大核心AOP系统梳理

目录 1 事务管理 1.1 事务 1.2 Transactional注解 1.2.1 rollbackFor 1.2.2 propagation 2 AOP 基础 2.1 AOP入门 2.2 AOP核心概念 3. AOP进阶 3.1 通知类型 3.2 通知顺序 3.3 切入点表达式 execution切入点表达式 annotion注解 3.4 连接点 1 事务管理 1.1 事务…...

react、vue动态form表单

需求在日常开发中反复写form 是一种低效的开发效率&#xff0c;布局而且还不同这就需要我们对其封装 为了简单明了看懂代码&#xff0c;我这里没有组件&#xff0c;都放在一起&#xff0c;简单抽离相信作为大佬的你&#xff0c;可以自己完成&#xff0c; 一、首先我们做动态f…...

halcon程序如何导出C#文件

1.打开halcon文件&#xff1b; 2.写好需要生成C#文件的算子或函数&#xff1b; 3.找到档案-输出&#xff0c;如下图&#xff1b; 4.点击输出&#xff0c;弹出如下窗口 &#xff08;1&#xff09;可以修改导出文件的存储路径 &#xff08;2&#xff09;选择C#-HALCON/.NET &…...

RabbitMQ三、springboot整合rabbitmq(消息可靠性、高级特性)

一、springboot整合RabbitMQ&#xff08;jdk17&#xff09;&#xff08;创建两个项目&#xff0c;一个生产者项目&#xff0c;一个消费者项目&#xff09; 上面使用原生JAVA操作RabbitMQ较为繁琐&#xff0c;很多的代码都是重复书写的&#xff0c;使用springboot可以简化代码的…...

第八十九周周报

学习目标&#xff1a; 论文 学习时间&#xff1a; 2024.05.25-2024.05.31 学习产出&#xff1a; 一、论文 SAN: INDUCING METRIZABILITY OF GAN WITH DISCRIMINATIVE NORMALIZED LINEAR LAYER 将GAN与切片最优输运联系起来&#xff0c;提出满足方向最优性、可分离性和单射…...

Centos升级Openssh版本至openssh-9.3p2

一、启动Telnet服务 为防止升级Openssh失败导致无法连接ssh&#xff0c;所以先安装Telnet服务备用 1.安装telnet-server及telnet服务 yum install -y telnet-server* telnet 2.安装xinetd服务 yum install -y xinetd 3.启动xinetd及telnet并做开机自启动 systemctl enable…...

茉莉香飘,奶茶丝滑——周末悠闲时光的绝佳伴侣

周末的时光总是格外珍贵&#xff0c;忙碌了一周的我们&#xff0c;终于迎来了难得的闲暇。这时&#xff0c;打开喜欢的综艺&#xff0c;窝在舒适的沙发里&#xff0c;再冲泡一杯香飘飘茉莉味奶茶&#xff0c;一边沉浸在剧情的海洋中&#xff0c;一边品味着香浓丝滑的奶茶&#…...

揭秘:Java字符串对象的内存分布原理

先来看看下面寄到关于String的真实面试题&#xff0c;看看你废不废&#xff1f; String str1 "Hello"; String str2 "Hello"; String str3 new String("Hello"); String str4 new String("Hello");System.out.println(str1 str2)…...

Vue.js - 生命周期与工程化开发【0基础向 Vue 基础学习】

文章目录 Vue 的生命周期Vue 生命周期的四个阶段Vue 生命周期函数&#xff08;钩子函数 工程化开发 & 脚手架 Vue CLI**开发 Vue 的两种方式&#xff1a;**脚手架目录文件介绍项目运行流程组件化开发 & 根组件App.vue 文件&#xff08;单文件组件&#xff09;的三个组成…...

Element-UI 快速入门指南

Element-UI 快速入门指南 Element-UI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发和维护。它提供了丰富的 UI 组件,帮助开发者快速构建美观、响应式的用户界面。本篇文章将详细介绍 Element-UI 的安装、配置和常用组件的使用方法,帮助你快速上手并应用于实际项…...

2024华为OD机试真题-整型数组按个位值排序-C++(C卷D卷)

题目描述 给定一个非空数组(列表),其元素数据类型为整型,请按照数组元素十进制最低位从小到大进行排序, 十进制最低位相同的元素,相对位置保持不变。 当数组元素为负值时,十进制最低位等同于去除符号位后对应十进制值最低位。 输入描述 给定一个非空数组,其元素数据类型…...

善听提醒遵循易经原则。世界大同只此一路。

如果说前路是一个大深坑&#xff0c;那必然是你之前做的事情做的不太好&#xff0c;当坏的时候&#xff0c;坏的结果来的时候&#xff0c;是因为你之前的行为&#xff0c;你也就不会再纠结了&#xff0c;会如何走出这个困境&#xff0c;是好的来了&#xff0c;不骄不躁&#xf…...

CrossOver有些软件安装不了 用CrossOver安装软件后如何运行

CrossOver为用户提供了三种下载软件的方式分别是&#xff1a;搜索、查找分类、导入。如果【搜索】和【查找分类】提供的安装资源不能成功安装软件&#xff0c;那么我们可以通过多种渠道下载安装包&#xff0c;并将安装包以导入的方式进行安装。这里我们以QQ游戏为例&#xff0c…...

在vue中如何使用leaflet图层展示地图

在vue中如何使用leaflet <template><div id"map" class"map"></div> </template><script> export default {data () {return {};},mounted(){this.initMaps()},methods: {initMaps () {const map L.map(map, {zoomControl…...

mybatisplus 字段存的是json 在查询的时候怎么映射成对象

数据库交互对象 TableName(value "表名", autoResultMap true)TableField(typeHandler JacksonTypeHandler.class, value "user_info")private User user;autoResultMap 是一个 MyBatis-Plus 中的注解属性&#xff0c;用于控制是否自动生成结果映射。…...

Python 学习笔记【1】

此笔记仅适用于有任一编程语言基础&#xff0c;且对面向对象有一定了解者观看 文章目录 数据类型字面量数字类型数据容器字符串列表元组 type()方法数据类型强转 注释单行注释多行注释 输出基本输出连续输出&#xff0c;中间用“,”分隔更复杂的输出格式 变量定义del方法 标识符…...

Git系列:rev-parse 使用技巧

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:「stormsha的主页」…...

【Java数据结构】详解LinkedList与链表(一)

&#x1f512;文章目录&#xff1a; 1.❤️❤️前言~&#x1f973;&#x1f389;&#x1f389;&#x1f389; 2.ArrayList的缺陷 3.链表的概念及结构 4.无头单向非循环链表的实现 4.1成员属性 4.2成员方法 createList display——打印链表 addFirst——头插 addLast…...