小编Wei*_*eng的帖子

方法签名中的抛出与Java中的抛出语句之间的区别

我试图弄清楚方法签名中的Throws和Java中的Throw Statements之间的区别.方法签名中的引发如下:

public void aMethod() throws IOException{
    FileReader f = new FileReader("notExist.txt");
}
Run Code Online (Sandbox Code Playgroud)

抛出语句如下:

public void bMethod() {
    throw new IOException();
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,throwsin方法签名是一种通知,该方法可能会抛出这样的异常.throw语句是在相应的情况下实际抛出创建的对象.从这个意义上讲,如果方法中存在throw语句,则应始终显示方法签名中的throws.

但是,以下代码似乎没有这样做.代码来自库.我的问题是它为什么会发生?我理解错误的概念吗?

这段代码是java.util.linkedList的副本.@author Josh Bloch

 /**
 * Returns the first element in this list.
 *
 * @return the first element in this list
 * @throws NoSuchElementException if this list is empty
 */
public E getFirst() {
    final Node<E> f = first;
    if (f == null)
        throw …
Run Code Online (Sandbox Code Playgroud)

java throw throws

33
推荐指数
2
解决办法
3万
查看次数

模块中的gradle aar library dependecy:无法解析

我是gradle的新手,我有一个依赖性的问题.我有以下项目结构:

-MyApp
-MyAppLibrary
-MyAppPro
-MyAppFree
-ThirdPartyLibraryWrapper
--libs\ThirdPartyLibrary.aar
Run Code Online (Sandbox Code Playgroud)

无论MyAppProMyAppFree依靠MyAppLibrary,这就要看ThirdPartyLibraryWrapper.顾名思义,它ThirdPartyLibraryWrapper是外部库的包装器,即ThirdPartyLibrary.aar.

这是我的配置:

build.gradle MyAppPro

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 8
        targetSdkVersion 22
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles 'proguard.cfg'
        }
    }
}

dependencies {
    compile project(':MyAppLibrary')
}
Run Code Online (Sandbox Code Playgroud)

build.gradle MyAppLibrary

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 22
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7 …
Run Code Online (Sandbox Code Playgroud)

android android-gradle-plugin

12
推荐指数
1
解决办法
9288
查看次数

使用FCM进行Android通知 - 谁启动了FirebaseMessagingService?

根据这里的设置指南,在示例应用程序中,A)我创建了一个extendsfirebase服务类的类.B)我把这些课程放在了AndroidManifest.xml

A)Java类

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //a) What's the life cycle of the service? How can I ensure this method is getting called?
        //b) If the service is killed at some point, will system restart it on receiving new messages?
        Log.d(TAG, "From: " + remoteMessage.getFrom());
    }
}
Run Code Online (Sandbox Code Playgroud)

B)AndroidManifest.xml

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

然后,该应用程序可以从FCM接收通知!

这是我的问题:

  1. 谁启动了FirebaseMessagingService通知服务?必须有一些地方调用startService(),不是吗?

  2. 我的理解是,无论我的应用程序状态如何,下游通知都会传送到我的移动设备的Google Play服务.然后,我可以保证什么程度,我onMessageReceive()被调用的时候,app或者serviceswipe …

notifications android android-service firebase firebase-cloud-messaging

11
推荐指数
1
解决办法
6825
查看次数

"类型不匹配:无法将int转换为字节"

我看到人们询问有关错误"类型不匹配:无法将int转换为字节"的问题.但它们主要是由所涉及的算术运算引起的.

这是我的情况:(
当我想在Eclipse Kepler中使用位时会发生这种情况)

//java 7 binary literals

byte a =  0b01111111; //8-bit it compiles 

byte b =  0b10000000;  //8-bit error: Type mismatch: cannot convert int to byte.                        

byte c =  (byte) 0b10000000; //8-bit it works fine if casted.
Run Code Online (Sandbox Code Playgroud)

问题是如果它是8位且最高位是1,那么编译器会给出错误.我想知道为什么.前缀0b表示它是二进制文字,那么为什么编译器将最高位数作为带符号的int数字或类似的东西呢?

谢谢回答.

[EDIT3:]

byte a = -128; //a = 0xFF = 11111111 (8 bits), compiler says ok.
byte b = 0b11111111; //compiler error
Run Code Online (Sandbox Code Playgroud)

[Edit2:按位&操作以某种方式触发错误]

byte a = 0b00000000;  //8 bits
a = (a&0xFF);  //gives same error: Type mismatch: cannot convert int …
Run Code Online (Sandbox Code Playgroud)

java byte bit-manipulation

9
推荐指数
1
解决办法
9427
查看次数

访问目标c中的类方法.使用自我或类名?

我正在学习iOS编程,并且对以下使用关键字self的代码感到困惑.

从我的理解来看,self就像Java一样this.它指的是当前实例.当我想调用一个类方法时,通常的方法应该是这样 [PlayingCard validSuits];但是在一个实例上入侵类方法也没关系,对吧?喜欢[self validSuits];(我在课堂上所以自我指的是PlayCard的一个实例)

但是在下面的代码中,它在某个地方给出了错误但在别处看起来没问题.(由3条评论指出,这是在Xcode 5.1中)

我错过了什么吗?

(PS我想我是有类似的问题在这里,这是任何一个解答.他甚至得到了使用[游戏牌validSuits]同样的错误.)

//  PlayingCard.m

#import "PlayingCard.h"

@implementation PlayingCard
@synthesize suit = _suit; 

+ (NSArray *)validSuits {
    return @[@"??", @"??", @"??", @"??"];
}

+ (NSArray *)rankStrings {
    return @[@"?", @"A", @"2", @"3", @"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];

}

+ (NSUInteger)maxRank {
    return [[PlayingCard rankStrings] count] -1; 
          //1. [self rankStrings] works fine.** 
}


//override super class's method
- (NSString *)contents {

    NSArray *rankStrings = [PlayingCard rankStrings];  
           //2. if change rankStrings …
Run Code Online (Sandbox Code Playgroud)

compiler-errors objective-c class-method ios

9
推荐指数
1
解决办法
9343
查看次数

Java - 新线程中的超级关键字Runnable - 通过类引用非静态方法

super我在网上找到的所有关键词教程中,很难让任何一个例子更接近于一个.我的问题:

  1. Tracker.super.track(event);和之间有什么区别test.parent.Tracker.track(event);

  2. 为什么第一个工作?

  3. 什么Tracker.super指的是?一个对象还是一个类?

子类:

package test;

public class Tracker extends test.parent.Tracker {


@Override
  public void track(final Event event) {
    Executor.execute(new Runnable() {
        public void run() {
            Tracker.super.track(event);  //this works!! why??
            super.track(event); // compile error: cannot resolve
            test.parent.Tracker.track(event);  //compile error: can only reference static method
        }
    });
  }

}
Run Code Online (Sandbox Code Playgroud)

超级

package test.parent;

public abstract class Tracker {

public void track(Event event) {}

}
Run Code Online (Sandbox Code Playgroud)

参考更新:

在jls8,15.11.2中

"假设字段访问表达式T.super.f出现在类C中,并且由T表示的类的直接超类是一个完全限定名称为S的类.如果S中的f可以从C访问,那么T.super .f被视为在类S的主体中是表达式this.f.否则,发生编译时错误.

因此,T.super.f可以访问类S中可访问的字段f,即使该字段被类T中的字段f的声明隐藏.

如果当前类不是类T或T本身的内部类,那么这是一个编译时错误."

java super runnable

6
推荐指数
1
解决办法
804
查看次数

使用子类对象访问超类函数

我有一个子类的对象扩展其超类.子类中有一个重写方法,可以使用该对象调用.是否可以使用子类对象调用超类的函数?

package supercall;

public class Main {

    public static void main(String[] args) {
        SomeClass obj = new SubClass();
        obj.go();   //is there anything like, obj.super.go()?
    }

}

class SomeClass {
    SomeClass() {

    }
    public void go() {
        System.out.println("Someclass go");
    }
}

class SubClass extends SomeClass {
    SubClass() {

    }
    @Override
    public void go() {
        System.out.println("Subclass go");
    }
}
Run Code Online (Sandbox Code Playgroud)

考虑上面的代码.

在这里打印

子类去

.相反,我必须打印

超级去

.

java inheritance

5
推荐指数
1
解决办法
2万
查看次数

在Java catch块中,您如何知道哪个方法/行抛出异常?

在try块中,我想执行两个函数.如果第一个失败则不执行第二个.我还想打印出哪个功能失败了.

请参阅以下代码.

try {
  a = func1();
  b = func2();  //won't execute if a failed
}
catch (Exception e) {
  //TODO: print a or b failed?
}
Run Code Online (Sandbox Code Playgroud)

语言是否自然地支持这种情况?

如果没有,以下是一个好的做法?(我想不出有什么不妥.但它让我担心,因为我不记得看到任何人使用returncatch.)

try {
  a = func1();
}
catch {
  //print: a failed
  return; //if a failed then skip the execution of b
}

try {
  b = func2();
}
catch {
  //print: b failed
}
Run Code Online (Sandbox Code Playgroud)

编辑:评论摘要:

  1. 从两种方法中抛出不同的异常.

    • 在这种情况下,方法是由其他人编写的,我无法控制.
  2. e.printStackTrace()将打印行号和功能

    • 我想做的不仅仅是打印.更像是,如果失败,请执行以下代码.

java exception try-catch

5
推荐指数
2
解决办法
4270
查看次数

gruntjs理解语法 - <%=小于百分比符号

以下是来自http://gruntjs.com/getting-started的示例gruntjs

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'build/<%= pkg.name %>.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');

  // Default task(s).
  grunt.registerTask('default', ['uglify']);

};
Run Code Online (Sandbox Code Playgroud)

然后提到:

由于<%%>模板字符串可能引用任何配置属性,因此可以通过这种方式指定配置数据(如文件路径和文件列表)以减少重复.

我的问题:

  1. <%= %>是什么意思?它是gruntjs语法还是在其他地方普遍使用?我在哪里可以找到它的定义?

  2. 您搜索神秘符号解释的一般方法是什么?如果我在google/stackoverflow中搜索这些字符串(" <%="," <%",包括引用与否),基本上不会出现合理的结果.

javascript javascript-objects gruntjs

4
推荐指数
1
解决办法
861
查看次数

为什么 Arrays.sort(T[] a, Comparator&lt;? super T&gt; c) 将 T 推断为二维数组的对象?

假设我想对二维数组进行排序。(只需重新排列行,不要触摸每行内的数据)。

在以下代码段中:所有 3 种情况都使用相同的Arrays.sort(T[] a, Comparator<? super T> c)方法签名。情况 (a) 工作正常。但是,只需在第二个参数中添加一个 if 条件,T 的推理就会发生变化。我无法理解为什么。

        // array contains 3 tuples, sort it by the first element, then second element
        int[][] array1 = new int[3][2];
        array1[0] = new int[]{1,2};
        array1[1] = new int[]{2,3};
        array1[2] = new int[]{2,4};

        // Case (a): compiles good, tuple is inferred as int[]
        Arrays.sort(array1, Comparator.comparingInt(tuple -> tuple[0]));  // Arrays.sort(T[] a, Comparator<? super T> c) correctly infers that T refers to int[]

        // Case (b.1): …
Run Code Online (Sandbox Code Playgroud)

java arrays comparator java-8

3
推荐指数
1
解决办法
79
查看次数