对synchronized(this)的一些理解

对synchronized(this)的一些理解
对synchronized(this)的一些理解

一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。

三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。

四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object

的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。

五、以上规则对其它对象锁同样适用.

举例说明:

一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

package ths;

public class Thread1 implements Runnable {

public void run() {

synchronized(this) {

for (int i = 0; i < 5; i++) {

System.out.println(Thread.currentThread().getName() + " synchronized l oop " + i);

}

}

}

public static void main(String[] args) {

Thread1 t1 = new Thread1();

Thread ta = new Thread(t1, "A");

Thread tb = new Thread(t1, "B");

ta.start();

tb.start();

}

}

结果:

A synchronized loop 0

A synchronized loop 1

A synchronized loop 2

A synchronized loop 3

A synchronized loop 4

B synchronized loop 0

B synchronized loop 1

B synchronized loop 2

B synchronized loop 3

B synchronized loop 4

二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。

package ths;

public class Thread2 {

public void m4t1() {

synchronized(this) {

int i = 5;

while( i-- > 0) {

System.out.println(Thread.currentThread().getName() + " : " + i);

try {

Thread.sleep(500);

} catch (InterruptedException ie) {

}

}

}

}

public void m4t2() {

int i = 5;

while( i-- > 0) {

System.out.println(Thread.currentThread().getName() + " : " + i);

try {

Thread.sleep(500);

} catch (InterruptedException ie) {

}

}

}

public static void main(String[] args) {

final Thread2 myt2 = new Thread2();

Thread t1 = new Thread(

new Runnable() {

public void run() {

myt2.m4t1();

}

}, "t1"

);

Thread t2 = new Thread(

new Runnable() {

public void run() {

myt2.m4t2();

}

}, "t2"

);

t1.start();

t2.start();

}

}

三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。

//修改Thread2.m4t2()方法:

public void m4t2() {

synchronized(this) {

int i = 5;

while( i-- > 0) {

System.out.println(Thread.currentThread().getName() + " : " + i);

try {

Thread.sleep(500);

} catch (InterruptedException ie) {

}

}

}

}

结果:

t1 : 4

t1 : 3

t1 : 2

t1 : 1

t1 : 0

t2 : 4

t2 : 3

t2 : 2

t2 : 1

t2 : 0

四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object

的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。

//修改Thread2.m4t2()方法如下:

public synchronized void m4t2() {

int i = 5;

while( i-- > 0) {

System.out.println(Thread.currentThread().getName() + " : " + i);

try {

Thread.sleep(500);

} catch (InterruptedException ie) {

}

}

}

结果:

t1 : 4

t1 : 3

t1 : 2

t1 : 1

t1 : 0

t2 : 4

t2 : 3

t2 : 2

t2 : 1

t2 : 0

五、以上规则对其它对象锁同样适用:

package ths;

public class Thread3 {

class Inner {

private void m4t1() {

int i = 5;

while(i-- > 0) {

System.out.println(Thread.currentThread().getName() + " : Inner.m4t1() =" + i);

try {

Thread.sleep(500);

} catch(InterruptedException ie) {

}

}

}

private void m4t2() {

int i = 5;

while(i-- > 0) {

System.out.println(Thread.currentThread().getName() + " : Inner.m4t2() =" + i);

try {

Thread.sleep(500);

} catch(InterruptedException ie) {

}

}

}

}

private void m4t1(Inner inner) {

synchronized(inner) { //使用对象锁

inner.m4t1();

}

}

private void m4t2(Inner inner) {

inner.m4t2();

}

public static void main(String[] args) {

final Thread3 myt3 = new Thread3();

final Inner inner = myt3.new Inner();

Thread t1 = new Thread(

new Runnable() {

public void run() {

myt3.m4t1(inner);

}

}, "t1"

);

Thread t2 = new Thread(

new Runnable() {

public void run() {

myt3.m4t2(inner);

}

}, "t2"

);

t1.start();

t2.start();

}

}

结果:

尽管线程t1获得了对Inner的对象锁,但由于线程t2访问的是同一个Inner中的非同步部分。所以两个线程互不干扰。

t1 : Inner.m4t1()=4

t2 : Inner.m4t2()=4

t1 : Inner.m4t1()=3

t2 : Inner.m4t2()=3

t1 : Inner.m4t1()=2

t2 : Inner.m4t2()=2

t1 : Inner.m4t1()=1

t2 : Inner.m4t2()=1

t1 : Inner.m4t1()=0

t2 : Inner.m4t2()=0

现在在Inner.m4t2()前面加上synchronized:

private synchronized void m4t2() {

int i = 5;

while(i-- > 0) {

System.out.println(Thread.currentThread().getName() + " : Inner.m4t2() =" + i);

try {

Thread.sleep(500);

} catch(InterruptedException ie) {

}

}

}

结果:

尽管线程t1与t2访问了同一个Inner对象中两个毫不相关的部分,但因为t1先获得了对Inner的对象锁,所以t2对Inner.m4t2()的访问也被阻塞,因为m4t2()是I nner中的一个同步方法。

t1 : Inner.m4t1()=4

t1 : Inner.m4t1()=3

t1 : Inner.m4t1()=2

t1 : Inner.m4t1()=1

t1 : Inner.m4t1()=0

t2 : Inner.m4t2()=4

t2 : Inner.m4t2()=3

t2 : Inner.m4t2()=2

t2 : Inner.m4t2()=1

t2 : Inner.m4t2()=0

listentothis英语中级听力答案解析修订版

l i s t e n t o t h i s英语中 级听力答案解析 HUA system office room 【HUA16H-TTMS2A-HUAS8Q8-HUAH1688】

英语中级听力参考答案 Answer Keys to Listen to This: 2 Edited by莫显良、马军军、张凤英、陈燕 L 1 L 2 L 3 L 4 L 5 L 6 L 7 L 8 L 9 L 10 L 11 L 12 L 13 L 14 L 15 L 16 L 17 L 18 L 19 L 20 L 21 L 22 L 23 L 24 L 25 L 26 L 27 L 28 L 29 L 30 L 31 L 32 L 33 L 34 L 35 L 36 Lesson 1 Section I Task 1: This Is Your Life! A.Choose the best answer (a, b or c) to complete each of the following statements. 1—6: caacba B.True or False Questions. 1—6: TFFFFT C.Identification.

(1)—(b), (2)—(d), (3)—(f), (4)—(g), (5)—(a), (6)—(c), (7)—(e) https://www.360docs.net/doc/0f2029284.html,plete the following résumé for Jason Douglas. Name: Jason Douglas Former name: Graham Smith Profession: actor Date of birth: July 2, 1947 1952: started school 1958: moved to Lane End Secondary School 1966: went to the London School of Drama 1969: left the London School of Drama 1973: went to Hollywood 1974: were in a movie with Maria Montrose Task 2: What Are Your Ambitions? A.Give brief answers to the following questions. 1.Radio Station QRX. 2.For a survey.

C++中explicit关键字的作用

C++中explicit关键字的作用 explicit用来防止由构造函数定义的隐式转换。 要明白它的作用,首先要了解隐式转换:可以用单个实参来调用的构造函数定义了从形参类型到该类类型的一个隐式转换。 例如: class things { public: things(const std::string&name = ""): m_name(name),height(0),weight(10){} int CompareTo(const things & other); std::string m_name; int height; int weight; }; 复制代码 这里things的构造函数可以只用一个实参完成初始化。所以可以进行一个隐式转换,像下面这样: things a; ................//在这里被初始化并使用。 std::string nm = "book_1"; //由于可以隐式转换,所以可以下面这样使用 int result = https://www.360docs.net/doc/0f2029284.html,pareTo(nm); 复制代码 这段程序使用一个string类型对象作为实参传给things的CompareTo函数。这个函数本来是需要一个tings对象作为实参。现在编译器使用string nm来构造并初始化一个things对象,新生成的临时的things对象被传递给CompareTo函数,并在离开这段函数后被析构。 这种行为的正确与否取决于业务需要。假如你只是想测试一下a的重量与10的大小之比,这么做也许是方便的。但是假如在CompareTo函数中还涉及到了要除以初始化为0的height 属性,那么这么做可能就是错误的。需要在构造tings之后更改height属性不为0。所以要限制这种隐式类型转换。 那么这时候就可以通过将构造函数声明为explicit,来防止隐式类型转换。

this关键字的作用

this关键字的作用 this关键字的用法 1. this是指当前对象自己。 当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。如下面这个例子中: public class Hello { String s = "Hello"; public Hello(String s) { System.out.println("s = " + s); System.out.println("1 -> this.s = " + this.s); this.s = s; System.out.println("2 -> this.s = " + this.s); } public static void main(String[] args) { Hello x="new" Hello("HelloWorld!"); } } 运行结果: s = HelloWorld! 1 -> this.s = Hello 2 -> this.s = HelloWorld! 在这个例子中,构造函数Hello中,参数s与类Hello的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果;第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld! 2. 把this作为参数传递 当你要把自己作为参数传递给别的对象时,也可以用this。如: public class A { public A() {

Java基础阶段复习题2

复习题2 如有雷同,纯属巧合! 1.下列哪一种叙述是正确的(D) A. abstract修饰符可修饰字段、方法和类 B.抽象方法的body部分必须用一对大括号{ }包住 C.声明抽象方法,大括号可有可无 D.声明抽象方法不可写出大括号 2.下列说法正确的有(C) A. class中的constructor不可省略 B. constructor必须与class同名,但方法不能与class同名 C. constructor在一个对象被new时执行 D.一个class只能定义一个constructor 3.以下哪个表达式是不合法的( A ) A、String x=”Hello”; int y=9; x+=y; B、String x=”Hello”; int y=9; if(x= =y) { } C、String x=”Hello”; int y=9; x=x+y; D、String x=null; int y=(x!=null)&&(x.length()>0) ? x.length() : 0 4.下列关于修饰符混用的说法,错误的是(C ) A.abstract不能与final并列修饰同一个类 B.abstract类中不可以有private的成员 C.abstract方法必须在abstract类中 D.static方法中能处理非static的属性 5.(B)修饰符允许对类成员的访问不依赖于该类的任何对象 A、abstract B、static C、return D、public 6.关于被私有访问控制符private修饰的成员变量,以下说法正确的是( C ) A.可以被三种类所引用:该类自身、与它在同一个包中的其他类、在其他包中的该类的子类 B.可以被两种类访问和引用:该类本身、该类的所有子类 C.只能被该类自身所访问和修改 D.只能被同一个包中的类访问

listen-to-this2-英语中级听力2答案

英语中级听力参考答案 Answer Keys to Listen to This: 2 Edited by莫显良、马军军、张凤英、陈燕 L 1L 2L 3L 4L 5L 6L 7L 8L 9L 10 L 11L 12L 13L 14L 15L 16L 17L 18L 19L 20 L 21L 22L 23L 24L 25L 26L 27L 28L 29L 30 L 31L 32L 33L 34L 35L 36 Lesson 1 Section I Task 1: This Is Your Life! A.Choose the best answer (a, b or c) to complete each of the following statements. 1—6: caacba B.True or False Questions. 1—6: TFFFFT C.Identification. (1)—(b), (2)—(d), (3)—(f), (4)—(g), (5)—(a), (6)—(c), (7)—(e) https://www.360docs.net/doc/0f2029284.html,plete the following résumé for Jason Douglas. Name: Jason Douglas Former name: Graham Smith Profession: actor Date of birth: July 2, 1947 1952: started school 1958: moved to Lane End Secondary School 1966: went to the London School of Drama 1969: left the London School of Drama 1973: went to Hollywood 1974: were in a movie with Maria Montrose Task 2: What Are Your Ambitions? A.Give brief answers to the following questions.

认识 C++ 中的 explicit 关键字

认识C++ 中的explicit 关键字 (Danny Kalev发表于2004-12-28 11:01:04) 带单一参数的构造函数在缺省情况下隐含一个转换操作符,请看下面的代码: class C { int i; //... public: C(int i);//constructor and implicit conversion operator //as well }; void f() { C c(0); c = 5; //将5 隐式转换为C 对象,然后赋值 } 编译器重新编辑上述例子代码,如下: ////////////////////////////////////////////////////////////////////////////////////////// //"c=5;" 被编译器转换成下面这个样子: ///////////////////////////////////////////////////////////////////////////////////////// C temp(5);// 实例化一个临时对象, c = temp; // 用= 赋值 temp.C::~C(); // temp 的析构函数被激活 在很多情况下,这个转换是有意的,并且是正当的。但有时我们不希望进行这种自动的转换,例如: class String { int size; char *p; //.. public: String (int sz); //这里不希望进行隐式转换操作 }; void f () { String s(10); // 下面是一个程序员的编码;发生一个意想不到的转换:

C# this关键字详解

在向大家详细介绍C# this关键字之前,首先让大家了解下类与对象的区分,然后全面介绍C# this 关键字。 类与对象的区分对我们把握OO编程至关重要。我们说类是对其成员的一种封装,但类的封装设计仅仅是我们编程的第一步,对类进行对象实例化,并在其数据成员上实施操作才是我们完成现实任务的根本。实例化对象采用MyClass myObject=new MyClass()语法,这里的new语义将调用相应的构建器。C#所有的对象都将创建在托管堆上。实例化后的类型我们称之为对象,其核心特征便是拥有了一份自己特有的数据成员拷贝。这些为特有的对象所持有的数据成员我们称之为实例成员。相反那些不为特有的对象所持有的数据成员我们称之为静态成员,在类中用static修饰符声明。仅对静态数据成员实施操作的称为静态函数成员。C#中静态数据成员和函数成员只能通过类名引用获取,看下面的代码: https://www.360docs.net/doc/0f2029284.html,ing System; 2.class A 3.{ 4.public int count; 5.public void F() 6.{ 7.Console.WriteLine(this.count); 8.} 9. 10.public static string name; 11.public static void G() 12.{ 13.Console.WriteLine(name); 14.} 15.} 16.class Test 17.{ 18.public static void Main() 19.{ 20.A a1=new A(); 21.A a2=new A(); 22.a1.F(); 23.a1.count=1; 24.a2.F(); 25.a2.count=2; 26. https://www.360docs.net/doc/0f2029284.html,="CCW"; 28.A.G(); 29.} 30.}

this关键字的含义

this关键字的含义 问题:this关键字的含义回答: this是一个保留字,仅限于构造函数和方法成员中使用 在类的构造函数中出现表示对正在构造的对象本身的引用,在类的方法中出现表示对调用该方法的对象的引用,在结构的构造上函数中出现表示对正在构造的结构的引用,在结构的方法中出现表示对调用该方法的结果的引用 this保留字不能用于静态成员的实现里,因为这时对象或结构并未实例化 在C# 系统中,this 实际上是一个常量,所以不能使用this++ 这样的运算 this保留字一般用于限定同名的隐藏成员、将对象本身做为参数、声明索引访问器、判断传入参数的对象是否为本身 示例: using System; using System.Collections.Generic; using System.Text; namespace Example10 { class Class1

private double c; private string value; public double C { get { return c; } } public Class1(double c) { //限定同名的隐藏成员 this.c = c; } public Class1(Class1 value) { //用对象本身实例化自己没有意义if (this != value) { c = value.C; }

public override string ToString() { //将对象本身做为参数 return string.Format(“{0} Celsius = {1} Fahrenheit”, c, UnitTransClass.C2F(this)); } //由于好奇,在这做了一个效率测试,想看看到底哪种方式访问成员变量更快,结论:区别不大。。。 public string Test1() { long vTickCount = Environment.TickCount; for (int i = 0; i this.value = i.ToString(); return string.Format(“Have this.: {0} MSEL”, Environment.TickCount –vTickCount); } public string Test2() { long vTickCount = Environment.TickCount; for (int i = 0; i value = i.ToString();

Listentothis3Lesson1答案及原文

News Item 1: 1. Gen eral comprehe nsion, choose the best an swer (a, b, c, or d) to complete each of the followi ng stateme nts ⑴ David Jacobse n was ___ b ___ . _ a. US colonel stationed in West Germany b. a freed America n hostage c. one of the rema ining captives in Leba non d. the man who worked to gain the release of hostages in Leba non ⑵ Terry Waite wan ted _ c _____ . _ a. to criticize the US government's handling of the hostage situation b. to have his checkups at the air force hospital c. to continue with his efforts to gain the release of other hostages d. to show his tha nks to the Reaga n adm ini strati on (3) The doctor's in itial impressi on is that _d . _ a. the hostage has heart-break ing disappo in tme nts b. the hostage has n't dealt well with the stresses of his captivity r c. the hostage is very tired both physically and men tally d. the hostage is physically in very good con diti on 2. Identification. Match each item in Column I with one item in Column II by recog nizing the pers on's occupati on or remarks Colu mn I (4) Preside nt Reaga n (1) Charles Moffitt Colum n II a. "There's no way to tell right ⑶ Terry Waite that."

英语高级听力listentothis3答案1到27单元

Lesson 1 Section one News item 1 A. b,c,d B. 1c,2d,3b,4a News item 2 A. b B. running behind News item 3 A. d, B. 1.president,had died in a plane crash 2.ruling,130 3.Foreign Minster,47 4.Portugal,1975 5.Prime Minister,9,transitional Section two A. FTFFF B. C,C,B,A,D,B,C,B C. 52,hospital director,married,good,US Air Force Hospital,Wiesbaden, W.Germany,this moring,undertermined,U.S.A D. 1.precisely,freedom 2.take up,reporters,six-mile jog 3.evaluation,had coped extremely well 4.evidence,tortured,physically ab used Section three A.1.Most Chinese thoughtt mao tsetong as a very good poet,according to the speaker. 2.poetry was considered abysmal because of the restriction of publicatio n during the ten years of the cultural revolution. 3.leaders in china,as well as in the east,are expected to be accomplished p oets. 4.it is about getting rid of a disease that was a plague in china. 5.willis barnstone is a professor of comparative literature at Indiana unive rsity in Bloomington. 6.mao’

static和this的理解和用法总结

static和this的理解和用法小结 关键字static和this是初学者比较头疼的知识点,自己也一直比较模糊.现在整理一下,既可以加深自己的印象也可以便于以后查询. 其实在think in java里关于为什么要使用static写的比较详细,不明白的多读几遍会有很大的收获.一般在两钟情形下需要使用static关键字:一种情形是只想用一个存储区域来保存一个特定的数据——无论要创建多少个对象,甚至根本不创建对象。另一种情形是我们需要一个特殊的方法,它没有与这个类的任何对象关联。也就是说,即使没有创建对象,也需要一个能调用的方法。一旦将什么东西设为static,数据或方法就不会同那个类的任何对象实例联系到一起.所以尽管从未创建那个类的一个对象,仍能调用一个static方法,或访问一些static数据。而在这之前,对于非static数据和方法,我们必须创建一个对象,并用那个对象访问数据或方法。这是由于非static数据和方法必须知道它们操作的具体对象.有这样的一个类,其中定义一个静态数据: class Test { Static int i = 47; } Test st1 = new StaticTest();Test st2 = new StaticTest();即使们new了两个Test对象,但它们仍然只占据Test.i的一个存储空间。这两个对象都共享同样的i。对方法来说,static一项重要的用途就是帮助我们在不必创建对象的前提下调用那个方法. 静态变量)一个静态对象属于一个类,但它不属于实例对象,也不是实例对象状态的一部分.每一个静态变量只存在一份.静态变量通常称为类变量(class variable).在实际中,经常需要这样的一个变量,它被一个类的所有实例对象所共享,如果它同时又是公有的,那么它就可以被这个类的任意访问者所使用.静态变量存在于类的作用域之内.通常声明为private.java中许多时候会用到public static final 这样的变量。静态变量可以被位于同一个作用域内的任意方或静态方法访问,访问时使用变量名称即可。如果在类作用域以外访问类,则要使用一个含有类名的表达式访问静态变量,例如: Integer.MAX_VALUE, 其中MAX_VALUE是在类库中声明过的。 静态方法)静态方法或类方法属于一个而不是属于某个实例对象实现的一部分。可以直接通过类来调用这种方法,而并不是只能由某个特定的实例对象调用。静态的方法不能用abstract声明,而且无论是否明确地指定实际上都是final型的。静态方法的声明格式: modifiers static typeName methodName (parameterList){ statementSequence } modifiers(可以从public,protect,private中选择一个),后面可以加上 final,nativc,synchronized中的一个或几个的组合。 static main是静态方法的一个特殊用法,用static main 方法来建立程序的初始状态,创建一组初始对象,并进行合理的方法调用,使得程序能够继续执行下去,static main方法使用String数组型参数包含了用户在运行时给出的任意命令行参数。

listentothis英语中级听力答案及原文

l i s t e n t o t h i s英语中级听力答案及原文 集团标准化办公室:[VV986T-J682P28-JP266L8-68PNN]

英语中级听力参考答案 Answer Keys to Listen to This: 2 Edited by莫显良、马军军、张凤英、陈燕 Lesson 1 Section I Task 1: This Is Your Life! A.Choose the best answer (a, b or c) to complete each of the following statements. 1—6: caacba B.True or False Questions. 1—6: TFFFFT C.Identification. (1)—(b), (2)—(d), (3)—(f), (4)—(g), (5)—(a), (6)—(c), (7)—(e) https://www.360docs.net/doc/0f2029284.html,plete the following résumé for Jason Douglas. Name: Jason Douglas Former name: Graham Smith Profession: actor Date of birth: July 2, 1947 1952: started school 1958: moved to Lane End Secondary School 1966: went to the London School of Drama 1969: left the London School of Drama 1973: went to Hollywood 1974: were in a movie with Maria Montrose Task 2: What Are Your Ambitions? A.Give brief answers to the following questions. 1.Radio Station QRX. 2.For a survey.

C 中的EXPLICIT关键字

c++中的explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢? 如果c++类的构造函数有一个参数,那么在编译的时候就会有一个缺省的转换操作:将该构造函数对应数据类型的数据转换为该类对象,如下面所示: class MyClass { public: MyClass(int num); } .... MyClass obj=10;//ok,convert int to MyClass 在上面的代码中编译器自动将整型转换为MyClass类对象,实际上等同于下面的操作: MyClass temp(10); MyClass obj=temp; 上面的所有的操作即是所谓的"隐式转换". 如果要避免这种自动转换的功能,我们该怎么做呢?嘿嘿这就是关键字explicit的作用了,将类的构造函数声明为"显示",也就是在声明构造函数的时候前面添加上explicit即可,这样就可以防止这种自动的转换操作,如果我们修改上面的MyClass类的构造函数为显示的,那么下面的代码就不能够编译通过了,如下所示: class MyClass { public: explicit MyClass(int num);

} .... MyClass obj=10;//err,can‘t non-explict convert class isbn_mismatch:public std::logic_error{public:explicit isbn_missmatch(const std::string &s):std:logic_error(s){}isbn_mismatch(const std::string&s,const std::string&lhs,const std::string &rhs):std::logic_error(s),left(lhs),right(rhs){}const std::string left,right;virtual~isbn_mismatch() throw(){}}; Sales_item&operator+(const Sales_item&lhs,const Sales_item rhs){if(!lhs.same_isbn(rhs)) throw isbn_mismatch("isbn missmatch",lhs.book(),rhs.book());Sales_item ret(lhs);ret+rhs;return ret;} Sales_item item1,item2,sum;while(cinitem1item2){try{sun=item1+item2;}catch(const isbn_mismatch&e){cerre.what()"left isbn is:"e.left"right isbn is:"e.rightendl;}} 用于用户自定义类型的构造函数,指定它是默认的构造函数,不可用于转换构造函数。因为构造函数有三种:1拷贝构造函数2转换构造函数3一般的构造函数(我自己的术语^_^) 另:如果一个类或结构存在多个构造函数时,explicit修饰的那个构造函数就是默认的 class isbn_mismatch:public std::logic_error{public:explicit isbn_missmatch(const std::string &s):std:logic_error(s){}isbn_mismatch(const std::string&s,const std::string&lhs,const std::string &rhs):std::logic_error(s),left(lhs),right(rhs){}const std::string left,right;virtual~isbn_mismatch() throw(){}}; Sales_item&operator+(const Sales_item&lhs,const Sales_item rhs){if(!lhs.same_isbn(rhs)) throw isbn_mismatch("isbn missmatch",lhs.book(),rhs.book());Sales_item ret(lhs);ret+rhs;return ret;}

深入浅出关键字---base和this

本文将介绍以下内容: ?面向对象基本概念 ?base关键字深入浅出 ?this关键字深入浅出 1. 引言 new关键字引起了大家的不少关注,尤其感谢Anders Liu的补充,让我感觉博客园赋予的交流平台真的无所不在。所以,我们就有必要继续这个话题,把我认为最值得关注的关键字开展下去,本文的重点是访问关键字(Access Keywords):base和this。虽然访问关键字不是很难理解的话题,我们还是有可以深入讨论的地方来理清思路。还是老办法,我的问题先列出来,您是否做好了准备。 1.是否可以在静态方法中使用base和this,为什么? 2.base常用于哪些方面?this常用于哪些方面? 3.可以base访问基类的一切成员吗? 4.如果有三层或者更多继承,那么最下级派生类的base指向那一层呢?例 如.NET体系中,如果以base访问,则应该是直接父类实例呢,还是最高 层类实例呢? 5.以base和this应用于构造函数时,继承类对象实例化的执行顺序如何? 2. 基本概念 base和this在C#中被归于访问关键字,顾名思义,就是用于实现继承机制的访问操作,来满足对对象成员的访问,从而为多态机制提供更加灵活的处理方式。 2.1 base关键字 其用于在派生类中实现对基类公有或者受保护成员的访问,但是只局限在构造函数、实例方法和实例属性访问器中,MSDN中小结的具体功能包括:?调用基类上已被其他方法重写的方法。 ?指定创建派生类实例时应调用的基类构造函数。 2.2 this关键字 其用于引用类的当前实例,也包括继承而来的方法,通常可以隐藏this,MSDN 中的小结功能主要包括: ?限定被相似的名称隐藏的成员 ?将对象作为参数传递到其他方法 ?声明索引器

笔试题——01

标签:Java面试题 一.选择题(共50题,每题2分,共100分。多选题选不全或选错都不得分。) 1.以下属于面向对象的特征的是(C,D)。(两项) A) 重载 B) 重写 C) 封装 D) 继承 2.以下代码运行输出是(C) publicclass Person{ private String name=”Person”; int age=0; } publicclass Child extends Person{ public String grade; publicstaticvoid main(String[] args){ Person p = new Child(); System.out.println(https://www.360docs.net/doc/0f2029284.html,); } } A) 输出:Person B) 没有输出 C) 编译出错 D) 运行出错 3.在使用super 和this关键字时,以下描述正确的是(A) A) 在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一行,否则编译不通过 B) super()和this()不一定要放在构造方法内第一行 C) this()和super()可以同时出现在一个构造函数中 D) this()和super()可以在static环境中使用,包括static方法和static语句块 4.以下对封装的描述正确的是(D) A) 只能对一个类中的方法进行封装,不能对属性进行封装 B) 如果子类继承了父类,对于父类中进行封装的方法,子类仍然可以直接调用 C) 封装的意义不大,因此在编码时尽量不要使用 D) 封装的主要作用在于对外隐藏内部实现细节,增强程序的安全性

Listen to this 3 Lesson 3答案与原文

News Item 1: 1. General Comprehension. Fill in the blanks to complete the following statements according to what you have heard. decision. 2. Spot Dictation. Listen to the tape again and fill in the following blanks. 3. Focusing on Details. Complete the following statement with details. The US State Department regrets the reductions of US private sector involvement in South Africa because they could

News Item 2: 1. General Comprehension. Complete the chart to provide a brief summary of the news item. Event: Demonstration 2. Focusing on Details. Fill the detailed information according to what you have heard. (3) Causes of the demonstration:

News Item 3: 1. General Comprehension. Fill in the blanks to complete the following statements according to what you have heard. 2. Summary. Use all the information you have gathered in Exercise 1 and try to write a summary for News Item 3. Suggested version: President Reagan met for about an hour today with West German Chancellor Helmut Kohl at the White House who expressed support for the President's SDI program. News in Detail General Comprehension. Choose the best answer (a, b, c, or d) to complete each of the following statements. 1. West German Chancellor Helmut Kohl ___________. a. has been in Washington D.C. for four days b. is in Washington D.C. for four days of meetings c. has been in Washington D.C. four hours before he talks with President Reagan d. is in Washington D. C. for a four-hour meeting 2.One of the following is on Kohl's agenda. It is ___________.

listentothis英语中级听力答案解析

英语中级听力参考答案 AnswerKeystoListentoThis:2 Editedby莫显良、马军军、张凤英、陈燕 L1L2L3L4L5L6L7L8L9L10 L11L12L13L14L15L16L17L18L19L20 L21L22L23L24L25L26L27L28L29L30 L31L32L33L34L35L36 Lesson1 SectionI Task1:ThisIsYourLife! A.Choosethebestanswer(a,borc)tocompleteeachofthefollowingstatements. 1—6:caacba B.TrueorFalseQuestions. 1—6:TFFFFT C.Identification. (1)—(b),(2)—(d),(3)—(f),(4)—(g),(5)—(a),(6)—(c),(7)—(e) https://www.360docs.net/doc/0f2029284.html,pletethefollowingrésuméforJasonDouglas. Name:JasonDouglas Formername:GrahamSmith Profession:actor Dateofbirth:July 2, 1947 1952:startedschool 1958:movedtoLane End Secondary School 1966:wenttotheLondon SchoolofDrama 1969:lefttheLondon SchoolofDrama 1973:wenttoHollywood 1974:wereinamoviewithMariaMontrose Task2:WhatAreYourAmbitions? A.Givebriefanswerstothefollowingquestions. 1.RadioStationQRX. 2.Forasurvey. 3.Four. 4.Six.

explicit关键字的作用

谈谈explicit关键字 2004-08-19 20:35 16677人阅读评论(7) 收藏举报 今天看到公司的代码内有大量的explicit关键字,但是老版的MSDN内例子并不完善,实在是不明白,最终从网上一篇文章内找到了答案:原来explicit是为了防止隐式使用拷贝构造函数的.以下附上从新版MSDN中找到的例子和网上那篇文章: // Copy From MSDN This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. The following program will fail to compile because of the explicit keyword. To resolve the error, remove the explicit keywords and adjust the code in g. // spec1_explicit.cpp // compile with: /EHsc #include class C { public: int i; explicit C(const C&) // an explicit copy constructor { printf("/nin the copy constructor"); } explicit C(int i ) // an explicit constructor { printf("/nin the constructor"); } C() { i = 0; } }; class C2 { public: int i;

相关文档
最新文档