我目前正在教自己关于课程,但遇到一个奇怪的错误,我必须做一些基本错误的事情,它一直说性别不在论证中,即使它是在男性之下。我的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.avantika.queueadmin.fragments.AddUserFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/editTextAddUserFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/editTextAddUserDisplayName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Display Name"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/editTextAddUserMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mobile No"
android:inputType="number|phone" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/editTextAddUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="text|textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/editTextAddUserAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:inputType="text|textMultiLine" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/editTextAddUserUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Spinner
android:id="@+id/spinnerAddUserGender"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnAddUserSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Add Employee" />
</LinearLayout>
</ScrollView>
答案 0 :(得分:1)
您编写的classification
函数是实例方法,这意味着它应该在类的实例上调用。您可以分两步完成:
bob = HumanClassification() # create the instance
bob.classification(32, 6, 'male') # call the method on it
但是将参数直接传递给__init__
可能更有意义,而不是使用单独的方法。如果希望可以在不预先指定所有值的情况下创建实例,则可以提供默认参数。
class HumanClassification:
def __init__(self, age=0, height=0, gender=''):
self.age = age
self.height = height
self.gender = gender
使用列表作为默认参数is usually a bad idea,因此我使用空字符串作为gender
的默认值。
另一种方法是将classification
更改为classmethod
,而不是常规方法。正如您目前所做的那样,classmethod
通常直接在课堂上调用。一些类设计有使用classmethod
实现的替代构造函数。这可能是这样的:
class HumanClassification:
def __init__(self):
self.age = 0
self.height = 0
self.gender = []
@classmethod # use the classmethod decorator
def classification(cls, age, height, gender): # first arg is the class, not an instance
self = cls() # create a new instance by calling the class
self.age = age
self.height = height
self.gender = gender
return self # return the instance
bob = HumanClassification.classification(32, 6, 'male') # this works now
print (bob.age)