Created
December 27, 2025 14:24
-
-
Save bahrom04/317da584721a1e9f93351ab53c891b94 to your computer and use it in GitHub Desktop.
Python ABC va Rust trait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from abc import ABC, abstractmethod | |
| class Animal(ABC): | |
| def __init__(self, name): | |
| self.name = name | |
| @abstractmethod | |
| def speak(self): | |
| print("hayvon buni gapirdi: ", self.name) | |
| class Sheep(Animal): | |
| def __init__(self, name): | |
| super().__init__(name) | |
| def speak(self): | |
| super().speak() | |
| print("Try programiz.pro") | |
| qoy = Sheep("aaahhhh") | |
| qoy.speak() | |
| # hayvon = Animal("aaahhhh") | |
| # hayvon.speak() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Animal { | |
| fn new(name: &'static str, naked: bool) -> Self; | |
| fn name(&self) -> &'static str; | |
| fn speak(&self) { | |
| println!("Hello, world!. Qoʻyni ismi {}", self.name()); | |
| } | |
| } | |
| struct Sheep { | |
| naked: bool, | |
| name: &'static str, | |
| } | |
| impl Animal for Sheep { | |
| fn new(name: &'static str, naked: bool) -> Sheep { | |
| Sheep { naked, name } | |
| } | |
| fn name(&self) -> &'static str { | |
| self.name | |
| } | |
| } | |
| fn main() { | |
| let qoʻy: Sheep = Animal::new("qoʻy", false); | |
| qoʻy.speak(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment