Vb.net Public Sub hungrys(gutom)未返回正确的输出
程序应返回消息“如果输入年龄大于0,则动物园动物饿了,否则动物园动物不饿”,但当我尝试运行程序时,结果始终为“动物园动物饿了”,即使您的输入为0。请参考代码。但当我尝试使用公共函数时,输出是正确的。但是我不能使用public函数,因为要求使用public sub() 末级Vb.net Public Sub hungrys(gutom)未返回正确的输出,vb.net,oop,Vb.net,Oop,程序应返回消息“如果输入年龄大于0,则动物园动物饿了,否则动物园动物不饿”,但当我尝试运行程序时,结果始终为“动物园动物饿了”,即使您的输入为0。请参考代码。但当我尝试使用公共函数时,输出是正确的。但是我不能使用public函数,因为要求使用public sub() 末级 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
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
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
-请看一看解释原因
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
你的例子是不完整的,很难从你的描述中准确地说出你想做什么,出了什么问题。目前,代码无法编译。但是,即使我们猜测了代码的其余部分是什么样子,它仍然没有实际调用
hungrys
方法,这似乎是您遇到的问题。请再次查看代码,我已经编辑了代码,以供参考。谢谢。您添加了对hungrys方法的调用(以及一些从未被调用过的不必要的方法),但我的原始观点仍然有效。它没有编译,您也没有清楚地解释它做错了什么,您还尝试了什么,以及它是如何出错的,您需要它做什么。例如,您从未设置年龄或饥饿布尔值,因此不清楚为什么您所设置的年龄或饥饿布尔值不起作用。我并不是故意刁难你。我很乐意帮忙。只是在这一点上,我真的不知道你不理解它的哪一部分。你需要先把你的值放到属性中,你不能只给它textbox3。text这有什么不同?
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