xxxxxxxxxx
// Use a simple object with a method
struct Person {
name:String,
}
impl Person {
fn new(name:&str) -> Self {Person{name:name.to_string()}}
pub fn greet(self: &Self, name: &str) -> String {
format!("Hello {}, my name is {}.", name, self.name)
}
}
fn main() {
// define variable based on struct
let p1 = Person::new("Able");
println!("{}", p1.name); // "Able"
// use a method
println!("{}", p1.greet("Baker")); // "Hello Baker, my name is Able"
}
xxxxxxxxxx
# Use a simple object with a method
class Person:
def __init__(self, name):
self.name = name
return
def greet(self, name):
return "Hello {}, my name is {}.".format(name, self.name)
# define variable based on class
p1 = Person("Able")
print(p1.name) # Able
# use a method
print(p1.greet("Baker")) # "Hello Baker, my name is Able"