var list = data.List<CtItem>.Where(c.CNumber == conNumber && c.Name == conName)
.OrderBy(o => o.CNumber)
rpt.DataSource = list;
rpt.DataBind();
}
我对Java很新。我正在阅读有关二传手的信息。我希望它能在代码的这一部分输出“Pablo”。
import java.util.Scanner;
class Description
{
String name = "Pablo";
public void getName(String newName) throws InterruptedException
{
System.out.println("Your name is ");
name = newName;
System.out.println(newName);
Thread.sleep(2000);
System.out.println("However, the creator of this crappy program is ");
System.out.println(this.name);
}
}
public class Learning {
public static void main(String[] args) throws InterruptedException
{
System.out.println("Description of yourself");
Scanner scan = new Scanner(System.in);
Thread.sleep(1000);
System.out.println("Please type your name");
Description person = new Description();
String myName = scan.nextLine();
person.getName(myName);
}
}
但是,假设我在代码的这一部分输入“Steve”
public void getName(String newName) throws InterruptedException
{
System.out.println("Your name is ");
name = newName;
System.out.println(newName);
Thread.sleep(2000);
System.out.println("However, the creator of this crappy program is ");
System.out.println(this.name);
}
应该将参数传递给方法getName,这就是。但是,当我到达部分时
String myName = scan.nextLine();
它打印出“Steve”,而它应该打印出“Pablo。所以控制台看起来像
System.out.println(this.name);
我做错了吗?
答案 0 :(得分:1)
它打印出“Steve”,而它应该打印出“Pablo。所以控制台看起来像
没有。在您的getName方法中,您将名称更改为给定名称。
name = newName;
因此,您的name
不再携带"Pablo"
,并且从此开始具有值"Steve"
。
答案 1 :(得分:1)
这是因为你有这一行
<ListView.ItemTemplateSelector>
<ts:CommentsTemplateSelector>
<!-- UserComment : with MenuFlyout -->
<ts:CommentsTemplateSelector.UserComment>
<DataTemplate>
<Border Tapped="Border_Tapped">
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
... />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
... />
</Grid>
...
</StackPanel>
<!-- MenuFlyout -->
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Edit"
Command="{Binding ElementName=CommentsPage, Path=DataContext.EditCommentCommand}" />
</MenuFlyoutItem>
<MenuFlyoutItem Text="Delete"
Command="{Binding ElementName=CommentsPage, Path=DataContext.DeleteCommentCommand}" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<local:OpenFlyoutAction />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Border>
</DataTemplate>
</ts:CommentsTemplateSelector.UserComment>
<!-- NoUserComment : without MenuFlyout -->
<ts:CommentsTemplateSelector.NoUserComment>
<DataTemplate>
<Border Background="Red">
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
... />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
... />
</Grid>
...
</StackPanel>
</Border>
</DataTemplate>
</ts:CommentsTemplateSelector.UserComment>
</ts:CommentsTemplateSelector>
<ListView.ItemTemplateSelector>
在使用name = newName;
打印之前,您使用newName替换了name的值。当您致电this.name
时,它会打印替换值。
在您完成打印名称之后,只需将名称替换为newName,而不是之前:
this.name
调用类的方法(函数)时,public void getName(String newName) throws InterruptedException
{
System.out.println("Your name is ");
System.out.println(newName);
Thread.sleep(2000);
System.out.println("However, the creator of this crappy program is ");
System.out.println(this.name);
name = newName;
}
仅相当于this.attribute_name
。
答案 2 :(得分:1)
我希望我的回答可以让您对变量的范围有所了解。
在内联评论中可以找到解释。
如果您不理解某些评论,请告诉我。
import java.util.Scanner;
public class Learning {
Description description;
public Learning(){
description = new Description();
}
/* app entry point */
public static void main(String[] args) throws InterruptedException
{
System.out.println("Description of yourself");
Scanner scan = new Scanner(System.in);
Thread.sleep(1000);
System.out.println("Please type your name");
Learning person = new Learning();
String myName = scan.nextLine();
person.description.getName(myName);
}
public class Description { // (0)
String name = "Pablo"; // name scope is from (0) to (1) so it can accessible from these places (*)
public String getName() {
// (*) here
return name;
}
public void setName(String name) {
// (*) here
this.name = name;
}
// newName has a reduced scope, so it can only be reached inside this method
// its scope goes from (2) to (3)
public void getName(String newName) throws InterruptedException{ // (2)
// (*) here
System.out.println("Your name is ");
// this line modifies the name outside of this method (which is where it was defined)
name = newName;
System.out.println("A: " + newName);
System.out.println("B: " + name);
// this line modifies the localName (since it was defined in this method) but not the one outside
// (because it is a new variable bound to this method scope)
String name = "RandomName";
System.out.println("C: " + newName);
System.out.println("D: " + name);
Thread.sleep(2000);
System.out.println("However, the creator of this crappy program is ");
// this refers to the class (Description) not the scope so
// this.name is the same as name in A: but not the same as the local-scoped name
System.out.println("E: " + this.name);
System.out.println("F: " + name);
} // (3)
} // (1)
}