文章目录
Java 中的变量有3种:
📖 ① 全局变量:被定义在类中(成员变量)
📖 ② 局部变量:被定义在成员方法、代码块、静态代码块中定义的变量
📖 ③ 参数:方法声明中的变量
There are several kinds of variables(变量):
📋 Member variables(成员变量) in a class:these are called fields(属性)
📋 Variables in a method or block of code(代码块):these are called local variables(局部变量)
📋 Variables in method declarations(方法声明):these are called parameters(参数)
✏️ 全局变量(成员变量)作用域为:整个类体
✏️ 局部变量(除全局变量之外的变量)作用域为:它所在的代码块
✏️ 全局变量可以不赋值,直接使用(全局变量有默认值)
✏️ 局部变量必须赋值后才能使用
✏️ 参数的值在方法被调用的时候才有
public class VariableDomain {
// 全局变量
private String name;
{
int age = 10; // 局部变量
}
static {
double pai = 3.14; // 局部变量
}
// num1、description 参数
public void test(int num1, String description) {
String hobby = "睡觉"; // 局部变量
}
}
📜 全局变量名和局部变量名可以一样,访问的时候遵循就近原则
📜 在同一作用域中(eg:同一成员方法中),不能有重名的局部变量
📜 同一类的同一代码块中的成员变量也不能重名
全局变量和局部变量重名的时候,访问遵循就近原则:
public class VariableDomain {
// 全局变量
private String name = "张浩男";
public static void main(String[] args) {
VariableDomain domain = new VariableDomain();
domain.test();
}
private void test() {
String name = "莫松"; // 局部变量(可以和全局变量重名)
// output: name = 莫松
System.out.println("name = " + name);
}
}
不同类的成员变量可以重名:

📜 全局变量(成员变量)的生命周期长:【对象就像一个人🙍,成员变量就像一个人的手👋】全局变量的生命伴随着对象的存在而存在,便随着对象的销毁而销毁
📜 局部变量生命周期短:与它所在的代码块共生
📜 全局变量可以在本类或其他类中使用
📜 局部变量只能在它所在类的指定方法中使用
📜 全局变量可以被修饰符修饰(eg:private、static、final)
📜 局部变量不能被修饰符修饰
📝 A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations:except that they use the name of the class and have no return type.
📝 ① 类中包含构造方法(可通过调用构造方法从一个类模板中创建一个对象)
📝 ② 声明构造方法和声明成员方法一样,但也有区别(✒️构造方法的方法名和类名一模一样;✒️构造方法没有返回值类型)
下面是一个构造方法的例子:
public class Student {
private String name;
private int age;
private double score;
// (1) 没有返回类型
// (2) 方法名和类名一致
public Student(String stuName, int stuAge, double stuScore) {
// 构造方法的方法体中一般给成员变量赋值
name = stuName;
age = stuAge;
score = stuScore;
}
}
📝 To create a new Student object called tom, a constructor is called by the new operator.
📝 要想创建一个名为 tom 的 Student 对象,可通过 new 运算符调用构造方法
new 运算符调用构造方法,创建 Student 对象:
Student zhn = new Student("张浩男", 12, 99.5);
📝 new Student("张浩男", 12, 99.5): creates space in memory(内存空间) for the object and initializes its fields
📝 new Student("张浩男", 12, 99.5): 该代码在内存中为对象开辟了内存空间,并且初始化了成员变量的值
📝 Although Student only has one constructor, it could have others, including a no-argument constructor(无参构造方法):
/**
* Student 类的无参构造方法
*/
public Student() {
name = "庆医";
age = 8;
score = 100;
}
📝 和成员方法一样,构造方法也是可以重载(Override)的:方法名和类名一致,参数列表各不相同
📝 Both constructors could have been declared in Student because they have different argument lists(参数列表). As with methods(与方法一样), the Java platform differentiates(区分) constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell(区分) them apart. Doing so causes a compile-time error.
📝 两个构造方法(有参构造方法和无参构造方法)都可在 Student 类中被声明,因为它们有不同的参数列表。与成员方法一样,Java 平台可通过参数数量和参数类型来区分构造方法。在同一个类中,你不能写两个拥有相同参数数量和参数类型的构造方法,因为 Java 平台无法区分。写两个拥有相同参数数量和参数类型的构造方法将会导致编译时错误。

💡 You don’t have to provide any constructors for your class, but you must be careful when doing this. The compiler(编译器) automatically provides a no-argument, default constructor(默认构造方法) for any class without constructors. This default constructor will call the no-argument constructor of the superclass(超类). In this situation, the compiler will complain(抱怨、埋怨、发恼骚) if the superclass(超类) doesn’t have a no-argument constructor so you must verify(验证) that it does. If your class has no explicit(明确的、显式的) superclass, then it has an implicit (隐式的、含蓄的)superclass of Object, which does have a no-argument constructor.
💡 你并不是必须得給你的类提供一个构造方法(你可以不同你的类提供任何构造方法)。若不给类提供任何构造方法的话,你必须当心一点。编译器会自动提供一个无参的默认构造方法,这个默认的无参构造方法是提供给那些没有任何构造方法的类的。这个默认的无参构造方法将会调用超类的无参构造方法,所以必须确保超类拥有无参构造方法,否则编译器会发恼骚(报错的)😡。如果你的类没有明确的超类,你的类会有一个隐式的超类(Object),Object 类是所有类的超类(是 Java 平台提供的),Object 类百分百拥有一个无参构造方法。
💚 You can use access modifiers(访问修饰符) in a constructor’s declaration to control which other classes can call the constructor.
💚 你可以在声明构造方法的使用使用访问修饰符(public、protected、private),用以控制哪些其他类可以调用这个构造方法。
💜 If another class cannot call a Student constructor, it cannot directly(直接地) create Student objects.
💜 如果其他类不能调用 Student 类的构造方法,其他类就无法直接地创建 Student 对象。
🍀 构造方法:也叫构造器,用于更方便地创建一个对象
🍀 方法名必须和类名一模一样
🍀 没有返回值类型
🍀 可以重载(方法名一样,方法签名不一样)
构造方法案例:
public class Handsome {
private int age;
private double weight;
public Handsome() {
}
public Handsome(int age) {
this.age = age;
}
public Handsome(int age, double weight) {
this.age = age;
this.weight = weight;
}
}
使用构造方法创建 Handsome 对象:
// 可通过3种构造方法创建 Handsome 对象
Handsome hs1 = new Handsome();
Handsome hs2 = new Handsome(17);
Handsome hs3 = new Handsome(17, 123.5);
🍀 this:指向当前对象的引用
this 的用途:
🍀 ① 访问当前类中定义的成员变量
🍀 ② 调用当前类中定义的方法(包括构造方法)
public class Computer {
private String brand;
private double price;
public Computer(String brand) {
// this 作用:调用当前类中定义的方法(包括构造方法)
this(brand, 0.0);
}
public Computer(String brand, double price) {
// this 作用:访问当前类中定义的成员变量
this.brand = brand;
this.price = price;
}
}
🍀 在类中直接写成员变量、直接调用成员方法,默认都是访问和调用当前对象的
public class Cat {
public int age;
public void eat() {
// 等价于:System.out.println(this.age + "eat()");
System.out.println(age + "eat()");
}
public void run() {
// 等价于:this.eat();
eat();
}
}
💙 this 的本质是一个隐藏的、位置最靠前的方法参数。(面向对象的语言的 this 都是这样的)

🌱 只能在构造方法中用 this 调用其他构造方法
🌱 如果在构造方法 A 中调用了其他构造方法,调用构造方法的语句必须是构造方法 A 中的第一条语句

🌼 默认构造方法(Default Constructor):如果一个类没有自定义构造方法,编译器会自动为它提供无参数的默认构造方法
🌼 一旦自定义了构造方法,默认构造方法就不再存在
public class Person {
private int age = 17;
private String name;
public Person(String name, int age) {
this.age = age;
this.name = name;
}
}
Person zhn = new Person("张浩男", 18);
对象创建流程:
🥤 ① 方法区加载一次类信息
🥤 ② 在堆中分配对象的内存空间
🥤 ③ 完成对象属性初始化(属性默认初始化;显示初始化;构造器初始化)
🥤 ④ 对象在堆中的内存地址赋值给 zhn(zhn 指向对象在堆中的地址)

Bye-Bye 如发现错误,请不吝赐教!
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是