Java Hello World通过CheckStyle(Java Hello World to pass CheckStyle)
所以我对使用checkstyle和我简单的HelloWorld java程序很陌生,我收到很多我不明白的错误。
我的代码:
package <package_name>; /** * A simple class to compile. */ public class HelloWorld { /** * @param args standard main parameters */ public static void main(String[] args) { System.out.println("hello world"); } }
我收到错误信息:
Line 6: Utility classes should not have a public or default constructor Line 10: Parameter args should be final
为什么会发生? 是否有必要为我的
main
类创建一个私有构造函数并使默认参数成为最终的?So I'm new to using checkstyle and for my simple HelloWorld java program, I'm receiving a lot of errors I don't understand.
My code:
package <package_name>; /** * A simple class to compile. */ public class HelloWorld { /** * @param args standard main parameters */ public static void main(String[] args) { System.out.println("hello world"); } }
I'm receiving the errors:
Line 6: Utility classes should not have a public or default constructor Line 10: Parameter args should be final
Why is this occurring? Is it necessary for me to create a private constructor for my
main
class and make the default arguments final?
原文:https://stackoverflow.com/questions/45447138
最满意答案
出于某种原因,ActiveRecord :: Relation忽略了
limit
选项。在Rails 3中,ActiveRecord实际上不会执行您的查询,直到需要访问结果。 调用
last
执行此操作(但同样会忽略限制)。您可以通过调用查询中的
all
来告诉ActiveRecord执行查询。 然后,当你last
运行时,它会给你你正在寻找的“最后”记录。@items = Item.where(:user_id => 1).order("updated_at DESC").limit(2) # @items is an ActiveRecord::Relation here @oldest_item = @items.last # Returns "C" instead of "B". This is actually ignoring the `limit` parameter @items = Item.where(:user_id => 1).order("updated_at DESC").limit(2).all # @items is an Array of ActiveRecord objects here @oldest_item = @items.last # Returns "B"
这对我来说似乎不是预期的行为。 我在rails问题跟踪器中提交了一个错误。
更新 :@BaroqueBobcat提交了一个被接受的补丁 ,因此应该在即将发布的Rails 3.1版本中修复。
For some reason, ActiveRecord::Relation is ignoring the
limit
option.In Rails 3, ActiveRecord doesn't actually execute your query until need to access the results. Calling
last
does this (but again, ignores the limit).You can tell ActiveRecord to execute the query by calling
all
on your query. Then, when you runlast
on that, it'll give you the "last" record you're looking for.@items = Item.where(:user_id => 1).order("updated_at DESC").limit(2) # @items is an ActiveRecord::Relation here @oldest_item = @items.last # Returns "C" instead of "B". This is actually ignoring the `limit` parameter @items = Item.where(:user_id => 1).order("updated_at DESC").limit(2).all # @items is an Array of ActiveRecord objects here @oldest_item = @items.last # Returns "B"
This doesn't seem like expected behavior to me. I've filed a bug in the rails issues tracker.
Update: @BaroqueBobcat submitted a patch which got accepted, so it should be fixed in the upcoming 3.1 release of Rails.
相关问答
更多-
出于某种原因,ActiveRecord :: Relation忽略了limit选项。 在Rails 3中,ActiveRecord实际上不会执行您的查询,直到需要访问结果。 调用last执行此操作(但同样会忽略限制)。 您可以通过调用查询中的all来告诉ActiveRecord执行查询。 然后,当你last运行时,它会给你你正在寻找的“最后”记录。 @items = Item.where(:user_id => 1).order("updated_at DESC").limit(2) # @items is ...
-
由于我在这篇文章中没有得到任何答案,我试图在其他论坛上找到其他人的答案。 我相信,无论是ActiveRecord(或MySQL的宝石)有一个错误。 里克詹姆斯说,如果我们想要得到最小的ID,我们应该使用有限的秩序。 这是他的回答 : http://forums.mysql.com/read.php?22,530328,530514#msg-530514 Since I didn't get any answer on this post, I tried to find the answer from ot ...
-
您应该将该方法添加为验证器,一旦验证器完成,您可以检查valid? 删除before_create然后添加: validate :allow_unique_only http://guides.rubyonrails.org/active_record_validations.html#custom-methods You should be adding the method as a validator, once the validator is done, you can check valid? ...
-
您正在尝试将实例方法作为类方法进行访问。 def self.reorder_action_items(new_order) new_order.each_with_index do |item, index| # here, self is not an instance of ActionList # and action_items is an instance method ai = self.action_items.find(item) ...
-
activity_date列实际上是一个日期,还是需要转换为日期的字符串? 如果是前者那么to_date是错位的,并且不应该存在。 @activities = Activity.where("user_id = ? AND activity_date = ?", @user.id, @activity_date) 它可能在一个环境而不是另一个环境中工作的原因是环境对nls_date_parameter具有不同的值,无论是对于数据库还是对于会话。 这里有用的诊断可能是解释计划,如果rails使用DBMS_X ...
-
通过查看rails github页面,然后从Rails 5.2开始, Migrator类被削减了很多。 其中大部分方法已转移到Migration类 (包括.up方法 )。 因此,只需用ActiveRecord::Migration替换ActiveRecord::Migrator 。 I managed to solve this (until Rails 6 is release at least), the implementation can be seen on textacular task :up ...
-
Rails Activerecord + MySQL查询没有给出我期望的结果(Rails Activerecord + MySQL query not giving the result I expect)[2021-07-14]
好的,我非常接近; def self.search_three(dyear) find_by_sql("SELECT model, SUM(IF(status = 'Completed',quantity,0)) AS Comp, SUM(IF(status = 'Shipped',quantity,0)) AS Ship, SUM(IF(status = 'Cart Submitted',quantity,0)) AS CS, SUM(quantity) AS quantity FROM orde ... -
您可以向Advertisement类添加2个新方法 def total_weight @total_weight ||= Advertisement.sum(:weight) end def weight_multiplier self.weight.to_f / total_weight end 然后你可以在Advertisement实例上调用weight_multiplier作为实例方法 You can add 2 new methods to your Advertisement clas ...
-
.find()和.find_by_xxx()为您提供用户实例,而.where()返回不是用户实例的ActiveRecord Relation。 如果你想从where()得到一个用户实例,你可以这样做: User.where(:user =>"andrata").first .find() and .find_by_xxx() give you a user instance while .where() returns ActiveRecord Relation that is not a user in ...
-
该范围背后的概念是错误的 。 范围的想法是始终返回ActiveRecord :: Relation的实例(对象的集合而不是一个)以允许与其他方法(例如where , includes , joins等)进一步链接。 如果您只需要从该范围中检索一个对象,则需要删除Order.last_from_this_year.first并将其用作: Order.last_from_this_year.first 。 其他解决方案是将该代码移动到类方法 : def self.last_from_this_year wh ...