两种类型的注释

本文介绍了C++中两种注释的基本用法:单行注释(//)和多行注释(/*...*/)。详细解释了它们的区别,并通过实例展示了如何在代码中合理运用注释来提高代码的可读性。

类型的评论

A comment is a line (or multiple lines) of text that are inserted into the source code to explain what the code is doing. In C++ there are two kinds of comments.

注释是一个线(或多线)的文本,插入到源代码解释代码是做什么的。在C++中有两种类型的注释。

The // symbol begins a C++ single-line comment, which tells the compiler to ignore everything to the end of the line. For example:

/ /符号开始一个C++单行注释,它告诉编译器忽略一切到终点。比如说呢。

1
cout << "Hello world!" << endl; // Everything from here to the right is ignored.

Typically, the single line comment is used to make a quick comment about a single line of code.

1
2
3
cout << "Hello world!" << endl; // cout and endl live in the iostream library
cout << "It is very nice to meet you!" << endl; // these comments make the code hard to read
cout << "Yeah!" << endl; // especially when lines are different lengths

Having comments to the right of a line can make the both the code and the comment hard to read, particularly if the line is long. Consequently, the // comment is often placed above the line it is commenting.

1
2
3
4
5
6
7
8
// cout and endl live in the iostream library
cout << "Hello world!" << endl;
  
// this is much easier to read
cout << "It is very nice to meet you!" << endl;
  
// don't you think so?
cout << "Yeah!" << endl;

The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the symbols is ignored.

1
2
3
/* This is a multi-line comment.
   This line will be ignored.
   So will this one. */

Since everything between the symbols is ignored, you will sometimes see programmers “beautify” their multiline comments:

1
2
3
4
/* This is a multi-line comment.
 * the matching asterisks to the left
 * can make this easier to read
 */

Multi-line style comments do not nest. Consequently, the following will have unexpected results:

1
2
/* This is a multiline /* comment */ this is not inside the comment */
//                                ^ comment ends here

规则:不要嵌套注释里面其他的评论

 

 

### 创建包含不同类型注释并处理静态和动态内容的JSP文件 为了满足需求,下面提供了一个具体的 JSP 文件示例,其中包含了单行注释、多行注释、静态内容以及动态内容。此例子还展示了如何通过简单的环境设置来运行这个 JSP 页面,并查看其源码以对比不同类型注释的区别。 #### JSP 文件 (`example.jsp`) ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Example Page</title> </head> <body> <!-- HTML Comment: This comment will be visible only in the source code --> <h1>Welcome to My Example Page!</h1> <p>This is a static paragraph.</p> <%-- JSP Multi-line Comment: The following lines are commented out and won't execute. They also do not appear as comments in the generated HTML output. --%> <% // Single line Java comment inside scriptlet, this does not show up anywhere except during development phase. /* Another way of writing multi-line Java comments within Scriptlets, these too remain hidden from both client-side view and final rendered HTML */ String message = "This text comes from server side."; %> <p><%=message%></p> <!-- Dynamic content inserted here --> </body> </html> ``` 上述代码片段中定义了多种注释方式: - **HTML 注释** 使用 `<!-- -->` 标签包裹,在浏览器端可见但在最终渲染后的网页上不可见[^1]。 - **JSP 多行注释** `<%-- --%>` 不会出现在生成的 HTML 中也不被执行[^3]。 - **Java 单行与多行注释** `%> // <%` 和 `%> /* */ <%` 只会在开发阶段起作用,既不会被发送给客户端也不会参与实际逻辑运算。 #### 设置简单环境运行 JSP 并验证效果 要测试这段代码的效果,需要有一个支持 JSP 的 Web 容器比如 Apache Tomcat 或者 Jetty 来部署应用。安装好之后按照官方文档启动服务并将上述 `.jsp` 文件放置于相应 webapps 目录下即可访问。 当打开浏览器输入 URL 访问该页面时(假设服务器地址为 localhost),可以看到如下输出: - 浏览器显示:“Welcome to My Example Page!”、“This is a static paragraph.” 以及 “This text comes from server side.” - 查看页面源码,则能发现仅存在 HTML 注释而其他形式的注释均未呈现。 这种差异正好体现了各种注释的作用范围及其特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值