我一直在努力理解java编译,我遇到了一个问题.假设我们有这样的课程设置
public class Ambiguity
{
static class G
{
void A()
{
System.out.println("Regular Method");
}
}
static class b
{
static void A()
{
System.out.println("Static Method");
}
}
public static void main(String[] args)
{
G b = new G();
b.A();
}
}
Run Code Online (Sandbox Code Playgroud)
编译器如何知道static void A()在类型对象上调用方法class b或常规方法的天气.基于我运行的测试它似乎调用对象(类型)方法但我不知道这是否始终是标准过程.在这种情况下,如何调用静态方法.void A()bGbG
这是在MVC 4中支持的默认AccountController中更改密码的代码:
// ChangePassword will throw an exception rather
//than return false in certain failure scenarios.
bool changePasswordSucceeded;
try
{
string userName = User.Identity.Name;
changePasswordSucceeded = WebSecurity.ChangePassword(userName,
model.OldPassword,
model.NewPassword);
}
catch (Exception)
{
changePasswordSucceeded = false;
}
if (changePasswordSucceeded)
{
return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
}
else
{
ModelState.AddModelError("",
"The current password is incorrect or the new password is invalid.");
}
Run Code Online (Sandbox Code Playgroud)
我的问题是消息不清楚.如果当前密码不正确那么没关系,但如果新密码无效,我想向用户提供更好的消息,告诉他们出了什么问题,我想更好地理解"失败情景",以便我可以定制消息.
这里的文档是关于异常的具体内容,我不认为这些内容应该在Action中被吞并并报告为无效密码.
那么为什么这里有"新密码无效"的可能性呢?如果我在我的应用程序中仅使用没有OAuth的SimpleMembershipProvider,我可以将其删除吗?
编辑: NB此数据注释也出现在"RegisterModel"类中,因此这里也有密码有效性检查
[StringLength(100,
ErrorMessage = "The {0} must be at least {2} …Run Code Online (Sandbox Code Playgroud) 我想在它们的节点中生成一个带键 - 值对的二叉树.
在我的二叉树中,我想在一开始用一个insert方法实现节点,如果密钥小于当前节点的密钥,则该方法实现一个新的左节点.然后,如果已经有一个左节点,它将再次检查它.右/大节点插入遵循相同的逻辑.
我首先使用int类型编写了我的代码,因为在我使用泛型之前,我更容易测试我的代码(对我来说是新主题).它在使用时起作用,int但我不确定如何使用"<"或">"将两个泛型与自身进行比较.
public ListCell<Type> checkKey(Type key, ListCell<Type> checkCell) {
ListCell<Type> newCell = null;
if (key < checkCell.key && checkCell.left != null) {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我不知道它是否值得说,但我正在使用自编码列表创建我的二叉树.在上面你可以看到我当前的检查,但我现在无法将我的给定密钥与checkCell.key进行比较,因为它们不是数字.
所以我的一般问题是如何比较泛型中的键,如果它们比我在二叉树中的实现"更小"或"更大".
提前致谢
我想从3-10或4-6等范围中选择一个随机数.应该选择数字,使得数字越低,选择的机会就越多.我下面的代码只选择具有相同概率的每个数字.
private int bonusPoints;
private double randomBonusPoints = Math.Random() * 100;
bonusPoints = (int)randomBonusPoints;
Run Code Online (Sandbox Code Playgroud)
如何从分布中选择P(3,4,5)=85%, P(6,7,8)=10%, P(9,10)=5%?
我正在制作一个cpu调度模拟器(用于学校项目).我的功能有问题roundRobin.当我这样做c.get(i).jobTime -= 2;,c.get(i).jobTime -= 1;它影响我的其他ArrayLists,所以我不能做我的其他功能.在我打电话之前roundRobin2我的名单是完全正常的.为什么会这样?
例如,这就是我的list4样子roundRobin2
list 4: [Job101 0, Job102 0, Job103 0, Job104 0, Job105 0, Job106 0]
这就是我在文件中读取的方式,并将Jobs对象放入我ArrayList的文件中.
Scanner input = new Scanner(new File("testdata1.txt"));
ArrayList<Jobs> list = new ArrayList<Jobs>();
ArrayList<Jobs> list2 = new ArrayList<Jobs>();
ArrayList<Jobs> list3 = new ArrayList<Jobs>();
ArrayList<Jobs> list4 = new ArrayList<Jobs>();
Jobs first;
while(input.hasNext())
{
first = new Jobs(input.next(), input.nextInt());
list.add(first);
list2.add(first);
list3.add(first);
list4.add(first);
}
input.close();
Run Code Online (Sandbox Code Playgroud)
这是我的 …
我只是从一本教科书中学习Java中的泛型,在那里它讨论了GenericStack<E>用一个实现的类ArrayList<E>.
因为为了创建一个字符串堆栈,您可以使用
GenericStack<String> = new GenericStack<String>()
Run Code Online (Sandbox Code Playgroud)
要么
GenericStack<String> new GenericStack<>()
Run Code Online (Sandbox Code Playgroud)
因此,不应该将构造函数
GenericStack定义为public GenericStack<E>(),或public GenericStack<>()?答案是不.它应该被定义为public GenericStack().
为什么是这样?显然,构造函数可以很容易地从类声明中推断出类型,但考虑到Java的冗长,我有点迷惑为什么<E>或者只是<>形式主义完全摆脱了这里.
在这个例子中:
using System;
using System.Collections.Generic;
using dbModel;
using System.Linq;
using System.Data.Entity.Infrastructure;
namespace WinApp
{
public partial class Form1 : Form
{
private dbEntities dbc;
public IQueryable<ARTIKLI> art;
public IQueryable<ART_GRUPE> grp;
public Form1()
{
InitializeComponent();
dbc = new dbEntities();
}
private void GetData()
{
art = from a in dbc.ARTIKLIs
select a;
grp = from g in dbc.ART_GRUPE
select g;
artikliBindingSource.DataSource = art.ToList();
artGrupeBindingSource.DataSource = grp.ToList();
}
private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
dbc.SaveChanges();
}
private void loadData_Click(object sender, EventArgs …Run Code Online (Sandbox Code Playgroud) 我有一个interface dest和一些实现这个接口的类:
class destImpl1 implements dest { ... }
class destImpl2 implements dest { ... }
Run Code Online (Sandbox Code Playgroud)
然后我有一个HashMap<dest,double> destHash. 我想要的是destHash像这样实例化:
destHash = new HashMap<destImpl1,double>();
Run Code Online (Sandbox Code Playgroud)
然后像这样:
destHash = new HashMap<destImpl2,double>();
Run Code Online (Sandbox Code Playgroud)
但代码无法编译。我在这里缺少什么?
我在下面的代码中的这一行收到错误:
listofmembers.add(Member);
Run Code Online (Sandbox Code Playgroud)
试图制作一个arraylist但是给我这个错误可能有人请帮我修复它谢谢你我想让我的库类保存我的成员和书籍,以便我可以打印它们作为一个列表
public class Library
{
private ArrayList<Member>listofmembers;
public Library()
{
listofmembers = new ArrayList<Member>();
}
public void storeMember(String Member)
{
listofmembers.add(Member);
}
public int numberOfMembers()
{
return Member.size();
}
}
Run Code Online (Sandbox Code Playgroud) java ×7
generics ×4
ambiguity ×1
arraylist ×1
c# ×1
compilation ×1
constructor ×1
list ×1
oop ×1
orm ×1
random ×1
range ×1
round-robin ×1
scheduling ×1
security ×1
syntax ×1
syntax-error ×1
winforms ×1