C++
类的定义和实例化调用
#include <iostream>
using namespace std;
class Animal // 定义类
{
public:
string color; // 类的属性
Animal(string color) // 类的构造函数,创建实例的时候调用
{
this->color = color; // 给类的属性赋值,用this表示实例本身
};
void eat() // 无参数无返回的函数定义
{
cout<<"动物在吃东西!"<<endl;
}
void run(string speed) // 有参数的函数定义
{
cout<<"动物以"+speed+"的速度在奔跑!"<<endl;
}
};
int main() {
Animal ani("白色"); // 类的实例化并传入参数
// Animal ani = Animal("白色"); // 和上面一行的作用一样
cout << ani.color <<endl; // 获取类的属性,输出:白色
ani.eat(); // 用实例调用函数,输出:动物在吃东西!
ani.run("3m/s"); // 调用的时候传参,输出:动物以3m/s的速度在奔跑!
return 0;
}
类的继承
#include <iostream>
using namespace std;
class Animal // 定义类
{
public:
string color; // 类的属性
Animal(string color) // 类的构造函数,创建实例的时候调用
{
this->color = color; // 给类的属性赋值
};
void eat() // 无参数无返回的函数定义
{
cout<<"动物在吃东西!"<<endl;
}
void run(string speed) // 有参数的函数定义
{
cout<<"动物以"+speed+"的速度在奔跑!"<<endl;
}
};
class Cat: public Animal // Cat类继承自Animal类
{
public:
string name;
Cat(string name, string color):Animal(color) // 调用父类的构造函数
{
this->name = name; // 子类特有的属性赋值
}
void eat() // 重写父类的eat函数
{
cout<<this->color+"的"+this->name+"在吃鱼!"<<endl;
}
};
int main() {
Cat cat("小白", "白色"); // 实例化的时候传入两个参数
cout << cat.color <<endl; // 获取父类已有的属性,输出:白色
cat.eat(); // 调用子类重写过的函数,输出:白色的小白在吃鱼!
cat.run("3m/s"); // 调用父类已有的函数,输出:动物以3m/s的速度在奔跑!
return 0;
}
类的多态
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void eat() // virtual关键字,如果子类重写了,子类的实例则会调用子类的函数
{
cout<<"动物在吃东西!"<<endl;
}
};
class Cat: public Animal // 继承自Animal类
{
public:
void eat() // 重写父类的eat函数
{
cout<<"小猫在吃鱼!"<<endl;
}
};
class Dog: public Animal // 继承自Animal类
{
public:
void eat() // 重写父类的eat函数
{
cout<<"小狗在啃骨头!"<<endl;
}
};
int main() {
Animal *animal; // 声明父类指针
Cat cat;
Dog dog; // Cat和Dog均继承自Animal类
// 通过父类的指针可以调用子类重写过的虚函数,实现多态
animal = &cat; // 指向Cat类的实例
animal->eat(); // 输出:小猫在吃鱼!
animal = &dog; // 指向Dog类的实例
animal->eat(); // 输出:小狗在啃骨头!
return 0;
}
Java
类的定义和实例化调用
class Animal{ // 定义类
String color; // 类的属性
Animal(String color){ // 类的构造函数,创建实例的时候调用
this.color = color; // 给类的属性赋值,用this表示实例本身
}
void eat(){ // 无参数无返回的函数定义
System.out.println("动物在吃东西!");
}
void run(String speed){ // 有参数的函数定义
System.out.println("动物以"+speed+"的速度在奔跑!");
}
}
public class JavaCode { // 主类入口
public static void main(String[] args){
Animal ani = new Animal("白色"); // 类的实例化并传入参数
System.out.println(ani.color); // 获取类的属性,输出:白色
ani.eat(); // 用实例调用函数,输出:动物在吃东西!
ani.run("3m/s"); // 调用的时候传参,输出:动物以3m/s的速度在奔跑!
}
}
类的继承
用extends关键字进行继承
class Animal{ // 定义类
String color; // 类的属性
Animal(String color){ // 类的构造函数,创建实例的时候调用
this.color = color; // 给类的属性赋值,用this表示实例本身
}
void eat(){ // 无参数无返回的函数定义
System.out.println("动物在吃东西!");
}
void run(String speed){ // 有参数的函数定义
System.out.println("动物以"+speed+"的速度在奔跑!");
}
}
class Cat extends Animal{ // Cat类继承自Animal类
String name; // 子类特有的属性
Cat(String name, String color){
super(color); // 调用父类的构造函数
this.name = name; // 子类特有的属性赋值
}
void eat(){ // 重写父类的eat函数
System.out.println(this.color+"的"+this.name+"在吃鱼!");
}
}
public class JavaCode { // 主类入口
public static void main(String[] args){
Cat cat = new Cat("小白","白色"); // 实例化的时候传入两个参数
System.out.println(cat.color); // 获取父类已有的属性,输出:白色
cat.eat(); // 调用子类重写过的函数,输出:白色的小白在吃鱼!
cat.run("3m/s"); // 调用父类已有的函数,输出:动物以3m/s的速度在奔跑!
}
}
类的多态
在继承的子类中,用Override关键字重写函数
class Animal{
void eat(){
System.out.println("动物在吃东西!");
}
}
class Cat extends Animal{
@Override
void eat(){ // 重写父类的eat函数
System.out.println("小猫在吃鱼!");
}
}
class Dog extends Animal{
@Override
void eat(){ // 重写父类的eat函数
System.out.println("小狗在啃骨头!");
}
}
public class JavaCode { // 主类入口
public static void main(String[] args){
Animal ani = new Animal();
Animal cat = new Cat(); // cat是Animal类型的变量,但是引用的对象是Cat类型
Animal dog = new Dog(); // Dog和Cat都继承自Animal类
// 子类的对象调用eat()函数的时候会调用子类重写过的函数
ani.eat(); // 输出:动物在吃东西!
cat.eat(); // 输出:小猫在吃鱼!
dog.eat(); // 输出:小狗在啃骨头!
}
}
Python
类的定义和实例化调用
class Animal(object): # 定义类,类名一般大写
def __init__(self, color): # 初始化,创建实例的时候调用
self.color = color # 将构造函数的参数赋给类的属性
def eat(self): # 至少传入一个参数self,代表实例本身
print("动物在吃东西!")
def run(self,speed): # 调用的时候可以传参
print("动物以"+speed+"的速度在奔跑!")
ani = Animal("白色") # 类的实例化并传入参数
print(ani.color) # 获取类的属性,输出:白色
ani.eat() # 用实例调用函数,输出:动物在吃东西!
ani.run("3m/s") # 调用的时候传参,输出:动物以3m/s的速度在奔跑!
其中有的操作不是必需的
class Animal(): # 括号里是父类名称,可以不写,默认继承object
def eat(self): # 可以没有初始化的定义,解释器调用默认的构造函数
print("动物在吃东西!")
def run(self,speed):
print("动物以"+speed+"的速度在奔跑!")
类的继承
class Animal():
def __init__(self, color):
self.color = color
def eat(self):
print("动物在吃东西!")
def run(self,speed):
print("动物以"+speed+"的速度在奔跑!")
class Cat(Animal): # Cat类继承自Animal类
def __init__(self, name, color):
super(Cat,self).__init__(color) # 调用父类的构造函数
self.name = name # 子类自己的属性
def eat(self): # Cat类中重写eat()函数
print(self.color+"的"+self.name+"在吃鱼!")
cat = Cat( "小白","白色") # 传入name和color两个参数
print(cat.color) # 获取调用父类的属性,输出:白色
cat.eat() # 调用子类重写过的函数,输出:白色的小白在吃鱼!
cat.run("3m/s") # 调用父类的函数,输出:动物以3m/s的速度在奔跑!
类的多态
class Animal():
def eat(self):
print("动物在吃东西!")
class Cat(Animal): # Cat类继承自Animal类
def eat(self): # Cat类中重写eat()函数
print("小猫在吃鱼!")
class Dog(Animal): # Dog类继承自Animal类
def eat(self): # Dog类中重写eat()函数
print("小狗在啃骨头!")
def objEat(obj): # 传入对象,调用对象的.eat()方法
obj.eat()
cat = Cat()
dog = Dog()
# 多态:同一个函数,传入不同的对象,实现不同的调用
objEat(cat) # 输出:小猫在吃鱼!
objEat(dog) # 输出:小狗在啃骨头!