这实际上是我困惑的一个面试问题的一部分。
class A
{
public A()
{
System.out.println("A") ;
}
}
class B extends A
{
public B()
{
System.out.println("B") ;
}
}
A a1 = new B();
System.out.println() ;
A a2 = (A) new B() ;
所以问题是什么是打印输出?
起初我认为它应该像
一样打印出来B
A
B
A
但是在我跑回家后,它给出了
A
B
A
B
我理解它是继承,然后将B向上转换为A,它也是合法的语法,但为什么在B之前打印A?
答案 0 :(得分:7)
为什么在B之前打印A?
因为超类构造函数的主体在子类构造函数的主体之前执行,基本上。
将app.controller('TestController', function ($scope) {
$scope.user = {key: 'j16er48', name: 'Foo', title: 'Bar'};
var secondUser = {key: false, name: 'Foo', title: 'Bar'};
var tmp;
$scope.changeUser = function () {
tmp = $scope.user;
$scope.user = secondUser;
secondUser = tmp;
};
});
构造函数隐式地视为:
B()
完整的详细信息位于JLS 12.5。特别是:
此构造函数不是以同一个类中的另一个构造函数的显式构造函数调用开始的(使用
public B() { super(); // This invokes A's constructor System.out.println("B") ; }
)。如果此构造函数用于Object以外的类,则此构造函数将以超类构造函数的显式或隐式调用开始(使用this
)。使用这五个相同的步骤评估参数并递归处理超类构造函数调用。
答案 1 :(得分:2)
父类 # encoding: UTF-8
require 'rbconfig'
require 'cgi'
module Pod
module UserInterface
module ErrorReport
class << self
def report(exception)
<<-EOS
#{'――― MARKDOWN TEMPLATE ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――'.reversed}
### Command
```
#{original_command}
```
### Report
* What did you do?
* What did you expect to happen?
* What happened instead?
### Stack
```
CocoaPods : #{Pod::VERSION}
Ruby : #{RUBY_DESCRIPTION}
RubyGems : #{Gem::VERSION}
Host : #{host_information}
Xcode : #{xcode_information}
Git : #{git_information}
Ruby lib dir : #{RbConfig::CONFIG['libdir']}
Repositories : #{repo_information.join("\n ")}
```
### Plugins
```
#{plugins_string}
```
#{markdown_podfile}
### Error
```
#{exception.class} - #{exception.message}
#{exception.backtrace.join("\n")}
```
#{'――― TEMPLATE END ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――'.reversed}
#{'[!] Oh no, an error occurred.'.red}
#{error_from_podfile(exception)}
#{'Search for existing GitHub issues similar to yours:'.yellow}
#{issues_url(exception)}
#{'If none exists, create a ticket, with the template displayed above, on:'.yellow}
https://github.com/CocoaPods/CocoaPods/issues/new
#{'Be sure to first read the contributing guide for details on how to properly submit a ticket:'.yellow}
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md
Don't forget to anonymize any private data!
EOS
end
private
def `(other)
super
rescue Errno::ENOENT => e
"Unable to find an executable (#{e})"
end
def pathless_exception_message(message)
message.gsub(/- \(.*\):/, '-')
end
def markdown_podfile
return '' unless Config.instance.podfile_path && Config.instance.podfile_path.exist?
<<-EOS
### Podfile
```ruby
#{Config.instance.podfile_path.read.strip}
```
EOS
end
def error_from_podfile(error)
if error.message =~ /Podfile:(\d*)/
"\nIt appears to have originated from your Podfile at line #{Regexp.last_match[1]}.\n"
end
end
def remove_color(string)
string.gsub(/\e\[(\d+)m/, '')
end
def issues_url(exception)
message = remove_color(pathless_exception_message(exception.message))
'https://github.com/CocoaPods/CocoaPods/search?q=' \
"#{CGI.escape(message)}&type=Issues"
end
def host_information
product, version, build = `sw_vers`.strip.split("\n").map { |line| line.split(':').last.strip }
"#{product} #{version} (#{build})"
end
def xcode_information
version, build = `xcodebuild -version`.strip.split("\n").map { |line| line.split(' ').last }
"#{version} (#{build})"
end
def git_information
`git --version`.strip.split("\n").first
end
def installed_plugins
CLAide::Command::PluginManager.specifications.
reduce({}) { |hash, s| hash.tap { |h| h[s.name] = s.version.to_s } }
end
def plugins_string
plugins = installed_plugins
max_name_length = plugins.keys.map(&:length).max
plugins.map do |name, version|
"#{name.ljust(max_name_length)} : #{version}"
end.sort.join("\n")
end
def repo_information
SourcesManager.all.map do |source|
next unless source.type == 'file system'
repo = source.repo
Dir.chdir(repo) do
url = `git config --get remote.origin.url 2>&1`.strip
sha = `git rev-parse HEAD 2>&1`.strip
"#{repo.basename} - #{url} @ #{sha}"
end
end
end
def original_command
"#{$PROGRAM_NAME} #{ARGV.join(' ')}"
end
end
end
end
end
的构造函数在子类A
之前调用。换句话说,它相当于:
B