Public Sub hungrys(gutom)没有返回正确的输出

时间:2017-09-28 11:45:52

标签: vb.net oop

程序应该返回消息“如果输入的年龄大于0则动物园动物很饿,否则动物园动物不饿”但是当我尝试运行程序时,结果总是“动物园动物饿了“即使您的输入为0.请参阅代码。但是当我尝试使用公共功能时输出是正确的。但我不能使用公共函数,因为要求是使用public sub()。

Public Class ZooAnimal
Private name As String
Private type As String
Private age As Integer
Private hungry As Boolean
Public Sub New()
    name = "Brown"
    type = ""
    age = 10
    hungry = isHungry()
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal age As Integer, ByVal hungry As Boolean)
    Me.name = name
    Me.type = type
    Me.age = age
    Me.hungry = isHungry()
End Sub
Public Function getName() As String
    Return name
End Function
Public Function getype() As String
    Return type
End Function
Public Function getAge() As Integer
    Return age
End Function
Public Function isHungry() As Boolean
    If age > 0 Then
        hungry = True
    Else
        hungry = False
    End If
    Return hungry
End Function
Public Sub hungrys(ByRef gutom As String)
    If hungry = True Then
        gutom = "The zoo animal is hungry"
    Else
        gutom = "The zoo animal is not hungry "
    End If
End Sub

Public Sub eat(ByRef eating As String)
    eating = "The zoo animal is eating"
End Sub
Public Sub sleep(ByRef sleeping As String)
    sleeping = "The zoo animal is sleeping"
End Sub

结束班

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim za As ZooAnimal
Dim zaa As New ZooAnimal
Dim gutom As String = ""
Dim eating As String = ""

zaa.hungrys(gutom)
zaa.eat(eating)

za = New ZooAnimal(TextBox1.Text, TextBox2.Text, TextBox3.Text, zaa.isHungry)
    TextBox10.Text = za.getAge & " " & gutom & " "& eating

1 个答案:

答案 0 :(得分:0)

说实话,你的代码存在一些问题 - 我建议使用属性而不是函数来重写它。您还可以在属性中包含代码以生成所需的结果。当您获得动物饥饿状态时,不要跟踪实际变量,只需使用返回年龄信息的属性即可。

Public Class Form1

Private Sub Go()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Go()
End Sub

Public Class ZooAnimal
    Private _name As String
    Private _type As String
    Private _age As Integer
    Private _eating As Boolean
    Private _sleeping As Boolean

    Public Sub New()
        _name = "Brown"
        _type = ""
        _age = 10
    End Sub

    Public Sub New(ByVal name As String, ByVal type As String, ByVal age As Integer)
        _name = name
        _type = type
        _age = age
    End Sub

    Public Property Name As String
        Get
            Return _name
        End Get
        Set(value As String)
            _name = value
        End Set
    End Property

    Public Property Type As String
        Get
            Return _type
        End Get
        Set(value As String)
            _type = value
        End Set
    End Property

    Public Property Age As Integer
        Get
            Return _age
        End Get
        Set(value As Integer)
            _age = value
        End Set
    End Property


    Public ReadOnly Property Hungrys() As String
        Get
            If Age > 0 Then
                Return "The zoo animal is hungry"
            Else
                Return "The zoo animal is not hungry "
            End If
        End Get
    End Property

    Public WriteOnly Property IsEating As Boolean
        Set(value As Boolean)
            _eating = value
        End Set
    End Property

    Public ReadOnly Property Eating As String
        Get
            If _eating Then
                Return "The zoo animal is eating"
            Else
                Return "The zoo animal is not eating"
            End If
        End Get
    End Property

    Public WriteOnly Property IsSleeping As Boolean
        Set(value As Boolean)
            _sleeping = value
        End Set
    End Property

    Public ReadOnly Property Sleeping As String
        Get
            If _sleeping Then
                Return "The zoo animal is sleeping"
            Else
                Return "The zoo animal is not sleeping"
            End If
        End Get
    End Property
End Class

您的按钮点击事件现在看起来像这样 - 顺便说一下,当您使用Visual Studio时,您应该将名为Option Strict的内容更改为On - 看看here到解释原因

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim za As ZooAnimal
    za = New ZooAnimal(TextBox1.Text, TextBox2.Text, CInt(TextBox3.Text))
    TextBox10.Text = za.Age & " " & za.Eating
End Sub