类中变量私有化和调用:__x和getx/setx或者property

本文详细介绍了Python中使用双下划线实现属性私有化的技巧,并通过三个实例展示了如何利用get和set方法、property属性及property装饰器来管理类的私有成员变量。

__xx:双前置下划线,子类不可继承属性、方法,父类私有。

详见:https://www.cnblogs.com/andy9468/p/8299448.html

 

例子1:隐藏数据:私有化后,用get和set方法

 1 class MoneyClass(object):
 2     def __init__(self):
 3         self.__money = 0
 4 
 5     def get_money(self):
 6         return self.__money
 7 
 8     def set_money(self, value):
 9         if isinstance(value, int):
10             self.__money = value
11         else:
12             print("error:不是整型数字")
13 
14 
15 m1 = MoneyClass()
16 print(m1.get_money())
17 m1.set_money(50)
18 print(m1.get_money())
View Code

输出:

0

50

 

例子2:property属性:自动调用get、set方法

 1 class MoneyClass(object):
 2     def __init__(self):
 3         self.__money = 0
 4 
 5     def get_money(self):
 6         return self.__money
 7 
 8     def set_money(self, value):
 9         if isinstance(value, int):
10             self.__money = value
11         else:
12             print("error:不是整型数字")
13 
14     getsetmoney = property(get_money, set_money)
15 
16 
17 m1 = MoneyClass()
18 print(m1.getsetmoney)
19 m1.getsetmoney = 800
20 print(m1.getsetmoney)
View Code

 输出:

0

800

 

例子3:property装饰器:自动调用get、set方法

 1 class MoneyClass(object):
 2     def __init__(self):
 3         self.__money = 0
 4 
 5     @property
 6     def getsetmoney(self):
 7         return self.__money
 8 
 9     @getsetmoney.setter
10     def getsetmoney(self, value):
11         if isinstance(value, int):
12             self.__money = value
13         else:
14             print("error:不是整型数字")
15 
16 
17 m1 = MoneyClass()
18 print(m1.getsetmoney)
19 m1.getsetmoney = 1000
20 print(m1.getsetmoney)
View Code

输出:

0

1000

 

转载于:https://www.cnblogs.com/andy9468/p/11138352.html

定义一个正n边形的。 在一个正n边形中,所有遍的长度都相等,且所有角的度数都相同(即这个多边形是等边等角的)。设计一个名为RegularPolygon的,该包括: (1)一个名为n的int型私有数据域,定义多边形的边数,默认值为3。 (2)一个名为side的double型私有数据域,存储边的长度,默认值为1。 (3)一个名为x的double型私有数据域,定义多边形的中点的x坐标,默认值为0。 (4)一个名为y的double型私有数据域,定义多边形的中点的y坐标,默认值为0。 (5)一个创建具有默认值的正多边形的无参构造方法。 (6)一个能创建带指定边数边长度、中心在(0,0)的正多边形的构造方法。 (7)一个能创建带指定边数边长度、中心在(x,y)的正多边形的构造方法。 (8)所有数据域的访问器修改器。 (9)一个返回多边形周长的方法getPerimeter()。 (10)一个返回多边形面积的方法getArea()。计算正多边形面积的公示是: 面积 编写一个测试程序,分别完成以下操作: (1)使用无参构造方法创建正多边形 (2)由用户输入边数边长度,创建中心在(0,0)的正多边形 (3)由用户输入边数、边长度及中心点坐标(x,y),创建正多边形 分别显示这三个对象的周长面积(面积显示时使用格式化控制台输出保留小数点后两位)。 输入输出样例如下: 输入语句格式为:(#表示空格,$表示换行) Enter#the#number#and#side#length#for#Polygon2:$ Enter#the#number,#side#length#and#center#point#coordinates#of#Polygon3:$ 输出语句格式为:(#表示空格,$表示换行) Polygon#1#perimeter:#3.0$ Polygon#1#area:#0.43$
04-13
目前activity_search_result.xml代码如下:<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:layout_marginTop="?attr/actionBarSize"> <!-- ====== 指南线:用于精确百分比定位 ====== --> <!-- 地图底部 --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_60" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.60" /> <!-- 结果列表顶部留白结束位置 --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_65" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.65" /> <!-- 按钮顶部位置 --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_90" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.90" /> <!-- 🔍 输入框 --> <AutoCompleteTextView android:id="@+id/search_input" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="请输入需要查询的公交线路或站点" android:textColorHint="#777777" android:textColor="@color/black" android:background="@drawable/rounded_edittext" android:minHeight="48dp" android:textSize="14sp" android:padding="12dp" android:singleLine="true" android:imeOptions="actionSearch" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/search_btn" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" /> <!-- 🔎 搜索按钮 --> <Button android:id="@+id/search_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toEndOf="@id/search_input" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" /> <!-- 🗺️ 地图视图 --> <com.amap.api.maps.MapView android:id="@+id/map_view" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="20dp" app:layout_constraintTop_toBottomOf="@id/search_input" app:layout_constraintBottom_toTopOf="@id/guideline_60" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 🔲 固定高度容器:包裹 RecyclerView --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/container_result_list" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="@id/guideline_65" app:layout_constraintBottom_toTopOf="@id/guideline_90" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginHorizontal="16dp"> <!-- 🔽 RecyclerView:内部可滚动 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/result_list" android:layout_width="0dp" android:layout_height="0dp" android:nestedScrollingEnabled="false" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:scrollbars="vertical" android:scrollbarThumbVertical="@drawable/custom_scrollbar_thumb" android:scrollbarTrackVertical="@drawable/custom_scrollbar_track"/> <!-- 空状态提示 --> <TextView android:id="@+id/empty_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂无搜索结果" android:visibility="gone" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <!-- “到这去”按钮 --> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="16dp" app:layout_constraintTop_toTopOf="@id/guideline_90" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <Button android:id="@+id/btn_toggle_mode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginEnd="8dp" android:text="📍 附近" android:textSize="14sp" android:padding="6dp" /> <Button android:id="@+id/btn_go_to" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginStart="8dp" android:text="到这去" android:textSize="14sp" /> </LinearLayout> <!-- 🌀 加载进度条 --> <ProgressBar android:id="@+id/progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:elevation="10dp" /> </androidx.constraintlayout.widget.ConstraintLayout> 目前fragment_home.xml代码如下:<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.home.HomeFragment" android:fitsSystemWindows="true" android:layout_marginTop="?attr/actionBarSize"> <!-- 🔵 垂直指南线:5%、50%、95% --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.05" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_50" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.50" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_95" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.95" /> <!-- 🔍 输入框 --> <AutoCompleteTextView android:id="@+id/home_input" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="请输入需要查询的公交线路或站点" android:textColorHint="#777777" android:completionThreshold="1" android:textColor="@color/black" android:background="@drawable/rounded_edittext" android:minHeight="48dp" android:textSize="14sp" android:padding="12dp" android:singleLine="true" android:imeOptions="actionSearch" app:layout_constraintTop_toTopOf="@id/guideline_05" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/home_search" app:layout_constraintHorizontal_chainStyle="packed" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" /> <!-- 🔎 搜索按钮 --> <Button android:id="@+id/home_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" app:layout_constraintTop_toTopOf="@id/home_input" app:layout_constraintBottom_toBottomOf="@id/home_input" app:layout_constraintStart_toEndOf="@id/home_input" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" /> <!-- 🚌 图片:居中偏上 --> <ImageView android:id="@+id/image_bus" android:layout_width="0dp" android:layout_height="wrap_content" android:src="@drawable/bus" app:layout_constraintTop_toTopOf="@id/guideline_50" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintWidth_percent="0.6" android:scaleType="fitCenter" /> <!-- ℹ️ 底部说明文字 --> <TextView android:id="@+id/text_home" android:textColor="#777777" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:textAlignment="center" android:textSize="11sp" app:layout_constraintTop_toTopOf="@id/guideline_95" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 我希望不改变原有的布局,并且修改后的搜索框,还能够像原来一样支持弹出提示
最新发布
12-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值