栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

几种编程语言里的结构体

Java 更新时间:发布时间: 百科书网 趣学号

文章目录
  • 写在前面
  • 内容
    • 结构体/类
      • Java
      • Dart
      • Rust
      • Go
    • 给结构体/类添加成员方法
      • Java
      • Dart
      • Rust
      • Go
    • 继承
      • Java
      • Dart
      • Rust
      • Go
    • 接口
      • Java
      • Dart
      • Rust
      • Go
  • 参考

写在前面

前段时间了解了一些 Rust 的东西,看到它给结构体(也就是 Java 或一些语言里所说的类)添加方法的时候,跟 Java 或者 Dart 里的不太一样。最近在看 Go,发现它也是如此。这一篇主要是对几种语言里结构体的一些东西进行简单的比较。

内容 结构体/类 Java
class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
Dart
class Person {
  late String name;
  late int age;

  Person(this.name, this.age);
}
Rust
struct Person {
    name: String,
    age: i16,
}
Go

Go 里的结构体需要 type和 struct两个关键字

type Person struct {
	name string
	age  int
}
给结构体/类添加成员方法 Java
public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
        
    }
}
Dart
class Person {
  late String name;
  late int age;

  Person(this.name, this.age);

  void sayHello() {}
}
Rust

Rust 里结构体的成员方法是在外面声明,通过 impl关键字加类名表示。成员方法里还需要 &self参数来表示是属于该类实例化后里的方法。

struct Person {
    name: String,
    age: i16,
}

impl Person {
    fn say_hello(&self) {}
}
Go

Go 和 Rust 有些相似,只是 Go 只需要声明方法,并在 func后带上类名,即可表示是类里的一个方法。

type Person struct {
	name string
	age  int
}

func (Person) sayHello() {

}
继承 Java
public class Student extends Person{
    public Student(String name, int age) {
        super(name, age);
    }
}
Dart
class Student extends Person {
  Student(String name, int age) : super(name, age);
}

Go 和 Rust 的所谓的面向对象不像 Java 和 Dart,所以它们在实现我们所说的“继承”上,会有些不一样。

Rust
struct Student {
    person: Person,
}
Go

Is Go an object-oriented language?
Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).
Also, the lack of a type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.

type Student struct {
	Person
}
接口 Java
public interface IAction {
    void doSomething();
}

class Person implements IAction{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public void doSomething() {

    }
}
Dart
abstract class IAction{
  void doSomething();
}

class Person implements IAction {
  late String name;
  late int age;

  Person(this.name, this.age);

  @override
  void doSomething() {

  }
}
Rust
// 定义接口
trait IAction {
    fn do_something(&self);
}
// 类实现接口
impl IAction for Person {
    fn do_something(&self) {}
}

struct Person {
    name: String,
    age: i16,
}
Go
// 定义接口
type IAction interface {
	doSomething()
}
// 实现接口,写法同样是声明方法的写法,只要方法名跟接口里的方法名一致,就认为实现了该接口
func (p Person) doSomething() {

}

type Person struct {
	name string
	age  int
}
参考

Is Go an object-oriented language?

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/957482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号