Ruby_访问控制(public,protected,private)

本文深入探讨了面向对象编程中的封装概念,通过具体代码示例解释了公有、私有和保护属性的使用场景及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

=begin
class test
def def1
puts "public"
end
def def2
puts "private"
end
def def3
puts "protected"
end
def def0
def1
def2
def3
end
public :def1 ,:def0
private :def2
protected :def3
end
=begin
test.def1
test.def2
test.def3
test.def0
#=end
test = test.new
#test.def1
#test.def2
#test.def3
#test.def0
class test1 <test

end
test1 = test1.new
test1.def1
test1.def0

class song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
end
#asong = song.new("hello","song",124)
#puts asong.inspect
#puts asong.to_s
class karaokesong < song
def initialize(name, artist, duration, lyrics)
super(name, artist, duration)
@lyrics = lyrics
end
end

asong = karaokesong.new("my way", "sinatra", 225, "and now, the...")
puts asong.inspect # "song: my way--sinatra (225)"
class test
def one
puts "私有"
end
def two
one
puts "共有"
end
protected : one
public : two
end
test = test.new
test.two

class person
def talk
puts " public :talk, 将调用speak"
speak
end

def speak
puts "protected:speak,将调用laugh"
laugh
end

def laugh
puts " private:laugh"
end

protected :speak
private :laugh
end
p1=person.new
p1.talk
#p1.speak 实例对象不能访问 protected 方法
#p1.laugh 实例对象不能访问 private 方法
class person
def speak
"protected:speak "
end

def laugh
" private:laugh"
end

protected :speak
private :laugh
end

class student < person

def uselaugh
puts laugh
end

def usespeak
puts speak
end

end

p2=student.new
p2.uselaugh
p2.usespeak

class person
def speak
"protected:speak "
end
def laugh
" private:laugh"
end
protected :speak
private :laugh
def uselaugh(another)
puts another.laugh
end
def usespeak(another)
puts another.speak
end
end
p1=person.new
p2=person.new

p2.usespeak(p1)
#p2.uselaugh(p1)
=end
class person
private #后面的方法设定为private
def talk
puts " already talk "
end
end

p1=person.new
p1.talk # private方法不能访问

class person
public :talk
end

p1.talk
<think>好的,我现在需要回答用户关于publicprotected访问修饰符区别的问题。首先,我需要回忆这两个关键字的定义在不同编程语言中的表现。根据用户提供的引用,引用[1]提到publicprivate的作用范围,public在任何地方可访问,而private仅在类内部。引用[2]讨论了Rubyprivateprotected的不同,特别是Rubyprivate方法只能在实例内部调用,不能指定接收者,而protected允许子类访问。引用[3]则展示了C#中protected成员可以在派生类中直接访问的例子。接下来,我需要整理publicprotected的基本区别。public修饰的成员在任何地方都可以访问,包括类外部、派生类其他类。而protected成员仅在类内部其派生类中可访问,外部无法直接访问。需要注意的是,不同语言可能有细微差别,比如Ruby中的privateC++中的private不同,而protected在C#C++中的表现也可能不同。 然后,我应该考虑用户可能的背景。用户可能对面向对象编程有一定了解,但想明确这两个修饰符的具体差异。可能需要举例说明,比如在C#或Java中的类继承情况,展示publicprotected成员的可访问性。同时,要指出跨语言差异,避免用户混淆不同语言的行为。还要注意引用中的信息,例如引用[3]提到C#的派生类可以直接访问基类的protected成员,而Rubyprotected允许子类访问父类的protected方法。这可能帮助用户理解不同语言中的具体实现。同时,引用[1]强调private不能修饰类名,这可能间接帮助用户理解访问修饰符的应用范围。 最后,需要生成相关问题,帮助用户进一步学习。问题应涵盖应用场景、跨语言差异、设计考量等,以促进深入理解。例如,何时使用protected,不同语言中的区别,以及设计模式中的应用。 总结时,要确保回答结构清晰,分点说明区别,提供代码示例,并指出语言差异,同时引用相关文献支持观点。确保使用正确的中文表达,避免技术术语使用不当,并保持回答的准确性易读性。</think>### Public Protected 访问修饰符的区别 #### 1. **作用范围** - **Public** 在任何位置均可访问,包括类外部、其他类及派生类。例如: ```java public class MyClass { public int publicVar = 10; } // 外部直接访问 MyClass obj = new MyClass(); System.out.println(obj.publicVar); // 允许 ``` - **Protected** 仅在类内部、**派生类**中可访问,外部无法直接调用。例如: ```java public class Base { protected int protectedVar = 20; } public class Derived extends Base { void accessProtected() { System.out.println(protectedVar); // 允许(派生类内) } } // 外部访问会报错 Base obj = new Base(); System.out.println(obj.protectedVar); // 错误 ``` #### 2. **跨语言差异** - **C++/C#/Java**: `protected` 允许派生类访问基类成员,但外部不可见[^3]。 - **Ruby**: `protected` 方法可在同类或派生类的实例间调用,但需隐式接收者(如直接调用,无需`self.method`)[^2]。 #### 3. **设计意图** - **Public** 用于暴露接口或公共数据,如工具类方法、API端点。 - **Protected** 用于实现继承逻辑,允许子类复用或扩展父类功能,同时封装细节。例如: ```csharp public class Animal { protected void Breathe() { /* 呼吸逻辑 */ } } public class Dog : Animal { public void Bark() { Breathe(); // 子类调用父类protected方法 } } ``` #### 4. **代码示例对比** - **Public 成员**:外部直接操作。 ```python class Car: def __init__(self): self.speed = 0 # 默认public(Python无显式修饰符) my_car = Car() my_car.speed = 100 # 允许 ``` - **Protected 成员**(约定使用`_`前缀): ```python class Vehicle: def __init__(self): self._engine_status = "off" # 约定为protected class Truck(Vehicle): def start_engine(self): self._engine_status = "on" # 子类可访问 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值